Class: Dapp::Dimg::Builder::Ansible

Inherits:
Base
  • Object
show all
Defined in:
lib/dapp/dimg/builder/ansible.rb

Defined Under Namespace

Classes: Assets

Constant Summary collapse

ANSIBLE_IMAGE_VERSION =
"2.4.4.0-10"

Instance Attribute Summary

Attributes inherited from Base

#dimg

Instance Method Summary collapse

Methods inherited from Base

#before_install, #before_install?, #before_install_checksum, #before_setup, #before_setup?, #before_setup_checksum, #build_artifact, #build_artifact?, #build_artifact_checksum, #initialize, #install, #install?, #install_checksum, #setup, #setup?, #setup_checksum

Constructor Details

This class inherits a constructor from Dapp::Dimg::Builder::Base

Instance Method Details

#ansible_binObject



7
8
9
# File 'lib/dapp/dimg/builder/ansible.rb', line 7

def ansible_bin
  "/.dapp/deps/ansible/#{ANSIBLE_IMAGE_VERSION}/embedded/bin/ansible"
end

#ansible_containerObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dapp/dimg/builder/ansible.rb', line 31

def ansible_container
  @ansible_container ||= begin
    is_container_exist = proc{dimg.dapp.shellout("#{dimg.dapp.host_docker} inspect #{ansible_container_name}").exitstatus.zero?}
    if !is_container_exist.call
      dimg.dapp.lock("dappdeps.container.#{ansible_container_name}", default_timeout: 600) do
        if !is_container_exist.call
          dimg.dapp.log_secondary_process(dimg.dapp.t(code: 'process.ansible_container_creating', data: {name: ansible_container_name}), short: true) do
            dimg.dapp.shellout!(
              ["#{dimg.dapp.host_docker} create",
               "--name #{ansible_container_name}",
               "--volume /.dapp/deps/ansible/#{ANSIBLE_IMAGE_VERSION} #{ansible_image}"].join(' ')
            )
          end
        end
      end
    end
    ansible_container_name
  end
end

#ansible_container_nameObject



27
28
29
# File 'lib/dapp/dimg/builder/ansible.rb', line 27

def ansible_container_name
  "dappdeps_ansible_#{ANSIBLE_IMAGE_VERSION}"
end

#ansible_imageObject



23
24
25
# File 'lib/dapp/dimg/builder/ansible.rb', line 23

def ansible_image
  "dappdeps/ansible:#{ANSIBLE_IMAGE_VERSION}"
end

#ansible_playbook_binObject



11
12
13
# File 'lib/dapp/dimg/builder/ansible.rb', line 11

def ansible_playbook_bin
  "/.dapp/deps/ansible/#{ANSIBLE_IMAGE_VERSION}/embedded/bin/ansible-playbook"
end

#ansible_playbook_solo_cmdObject



19
20
21
# File 'lib/dapp/dimg/builder/ansible.rb', line 19

def ansible_playbook_solo_cmd
  "#{ansible_playbook_bin} -c local"
end

#before_build_checkObject



163
164
# File 'lib/dapp/dimg/builder/ansible.rb', line 163

def before_build_check
end

#before_dimg_should_be_built_checkObject



166
167
# File 'lib/dapp/dimg/builder/ansible.rb', line 166

def before_dimg_should_be_built_check
end

#container_tmpdirObject

temporary directory for ansible



199
200
201
# File 'lib/dapp/dimg/builder/ansible.rb', line 199

def container_tmpdir
  dimg.container_dapp_path("ansible-tmpdir")
end

#container_workdirObject

directory with playbook in container



194
195
196
# File 'lib/dapp/dimg/builder/ansible.rb', line 194

def container_workdir
  dimg.container_dapp_path("ansible-workdir")
end

#create_workdir_structure(stage) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dapp/dimg/builder/ansible.rb', line 94

def create_workdir_structure(stage)
  @workdir_structures ||= {}
  @workdir_structures[stage.to_s] ||= true.tap do
    host_workdir(stage).tap do |workdir|
      # playbook with tasks for a stage
      workdir.join('playbook.yml').write YAML.dump(stage_playbook(stage))
      #puts YAML.dump(stage_playbook(stage))

      # generate inventory with localhost and python in dappdeps-ansible
      workdir.join('hosts').write Assets.hosts(python_path)

      # generate ansible config for solo mode
      workdir.join('ansible.cfg').write Assets.ansible_cfg(container_workdir.join('hosts'),
                                                           container_workdir.join('lib', 'callback'),
                                                           dimg.dapp.sudo_bin,
                                                           container_tmpdir.join('local'),
                                                           container_tmpdir.join('remote'),
                                                           )

      # save config dump for pretty errors
      workdir.join('dump_config.json').write JSON.generate(stage_config(stage)['dump_config'])

      # python modules
      workdir.join('lib').tap do |libdir|
        libdir.mkpath
        # crypt.py hack
        # TODO must be in dappdeps-ansible
        libdir.join('crypt.py').write Assets.crypt_py
        libdir.join('callback').tap do |callbackdir|
          callbackdir.mkpath
          callbackdir.join('__init__.py').write '# module callback'
          callbackdir.join('live.py').write Assets.live_py
          # add dapp specific stdout callback for ansible
          callbackdir.join('dapp.py').write Assets.dapp_py
        end
      end
    end
  end
end

#host_tmpdir(stage) ⇒ Object

temporary directories for ansible



182
183
184
185
186
187
188
189
190
191
# File 'lib/dapp/dimg/builder/ansible.rb', line 182

def host_tmpdir(stage)
  @host_tmpdirs ||= {}
  @host_tmpdirs[stage.to_s] ||= begin
    dimg.tmp_path(dimg.dapp.consistent_uniq_slugify(dimg.config._name || "nameless"), "ansible-tmpdir-#{stage.to_s}").tap do |p|
      p.mkpath
      p.join('local').mkpath
      p.join('remote').mkpath
    end
  end
end

#host_workdir(stage) ⇒ Object

host directory in tmp_dir with directories structure



174
175
176
177
178
179
# File 'lib/dapp/dimg/builder/ansible.rb', line 174

def host_workdir(stage)
  @host_workdirs ||= {}
  @host_workdirs[stage.to_s] ||= begin
    dimg.tmp_path(dimg.dapp.consistent_uniq_slugify(dimg.config._name || "nameless"), "ansible-workdir-#{stage.to_s}").tap {|p| p.mkpath}
  end
end

#python_pathObject



15
16
17
# File 'lib/dapp/dimg/builder/ansible.rb', line 15

def python_path
  "/.dapp/deps/ansible/#{ANSIBLE_IMAGE_VERSION}/embedded/bin/python"
end

#stage_config(stage) ⇒ Object

query tasks from ansible config create dump_config structure returns structure: { ‘tasks’ => [array of tasks for stage],

'dump_config' => {
   'dump_config_doc' => 'dump of doc',
   'dump_config_sections' => {'task_0'=>'dump for task 0', 'task_1'=>'dump for task 1', ... }}


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dapp/dimg/builder/ansible.rb', line 58

def stage_config(stage)
  @stage_configs ||= {}
  @stage_configs[stage.to_s] ||= begin
    {}.tap do |stage_config|
      stage_config['dump_config'] = {
        'dump_config_doc' => dimg.config._ansible['dump_config_doc'],
        'dump_config_sections' => {},
      }
      stage_config['tasks'] = dimg.config._ansible[stage.to_s].map.with_index do |dapp_task, task_num|
        {}.tap do |task|
          task.merge!(dapp_task['config'])
          task['tags'] = [].tap do |tags|
            tags << dapp_task['config']['tags']
            dump_tag = "task_#{task_num}"
            tags << dump_tag
            stage_config['dump_config']['dump_config_sections'][dump_tag] = dapp_task['dump_config_section']
          end.flatten.compact
        end
      end || []
    end
  end
end

#stage_empty?(stage) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/dapp/dimg/builder/ansible.rb', line 169

def stage_empty?(stage)
  stage_config(stage)['tasks'].empty?
end

#stage_playbook(stage) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dapp/dimg/builder/ansible.rb', line 81

def stage_playbook(stage)
  @stage_playbooks ||= {}
  @stage_playbooks[stage.to_s] ||= begin
    [{
      'hosts' => 'all',
      'gather_facts' => 'no',
      'tasks' => [],
     }].tap do |playbook|
      playbook[0]['tasks'].concat(stage_config(stage)['tasks'])
    end
  end
end