Class: VagrantSubutai::Blueprint::AnsibleController

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-subutai/blueprint/ansible_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ansible, environment, url, token) ⇒ AnsibleController

Returns a new instance of AnsibleController.



12
13
14
15
16
17
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 12

def initialize(ansible, environment, url, token)
  @ansible = ansible
  @environment = environment
  @url = url
  @token = token
end

Instance Attribute Details

#ansibleObject

ansible model



7
8
9
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 7

def ansible
  @ansible
end

#environmentObject

ansible model



7
8
9
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 7

def environment
  @environment
end

#tokenObject

ansible model



7
8
9
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 7

def token
  @token
end

#urlObject

ansible model



7
8
9
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 7

def url
  @url
end

Instance Method Details

#downloadObject

Downloads ansible source



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 52

def download
  Put.info "\nStarted downloading ansible source...."
  response = Rest::SubutaiConsole.command_async("bash /root/get_unzip.sh #{@ansible.source_url}",
                                                @environment.ansible_host_id,
                                                "/root","360000",
                                                @url, @token)

  case response
    when Net::HTTPOK
      json = JSON.parse(response.body)
      track(json['id'])
    when Net::HTTPAccepted
      json = JSON.parse(response.body)
      track(json['id'])
    else
      Put.error response.body
      Put.error response.message
  end
end

#find(hostname) ⇒ Object

Finds Container model from array by hostname



169
170
171
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 169

def find(hostname)
  @environment.containers.find {|cont| cont.hostname.include?(hostname)}
end

#hostsObject

Adds template hosts to ansible configuration /etc/ansible/hosts



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 21

def hosts
  Put.warn "\nStarted configuring ansible hosts.......\n"
  @ansible.groups.each do |group|
    Put.info "Adding group [#{group['name']}]"

    response = Rest::SubutaiConsole.command("echo [#{group['name']}] >> /etc/ansible/hosts", @environment.ansible_host_id, "/root","1000", @url, @token)
    status(response)

    group['hostnames'].each do |hostname|
      container = find(hostname)
      Put.info "Adding hosts #{container.containerName} to group [#{group['name']}]"

      if group.key?('python-interpreter')
        response = Rest::SubutaiConsole.command("echo \"#{container.containerName} ansible_user=root template=#{hostname} ansible_ssh_host=#{container.ip} ansible_python_interpreter=#{group['python-interpreter']}\" >> /etc/ansible/hosts",
                                                @environment.ansible_host_id,
                                                "/root",
                                                "360000",
                                                @url, @token)
        status(response)
      else
        response = Rest::SubutaiConsole.command("echo \"#{container.containerName} ansible_user=root template=#{hostname} ansible_ssh_host=#{container.ip}\" >> /etc/ansible/hosts",
                                                @environment.ansible_host_id,
                                                "/root","360000",
                                                @url, @token)
        status(response)
      end
    end
  end
end

#runObject

Runs ansible playbook



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 131

def run
  Put.info "\nStarted running ansible playbook may be take too long time please wait......."
  if @ansible.extra_vars.empty?
    response = Rest::SubutaiConsole.command_async("cd /root/*master/;ansible-playbook #{@ansible.ansible_playbook}", @environment.ansible_host_id, "/root","360000", @url, @token)

    case response
      when Net::HTTPOK
        json = JSON.parse(response.body)
        track(json['id'])
      when Net::HTTPAccepted
        json = JSON.parse(response.body)
        track(json['id'])
      else
        Put.error response.body
        Put.error response.message
    end
  else
    extra_vars = {}
    @ansible.extra_vars.each do |extra_var|
      extra_var.map {|k, v| extra_vars[k] = v }
    end
    response = Rest::SubutaiConsole.command_async("cd /root/*master;ansible-playbook #{@ansible.ansible_playbook} --extra-vars '#{extra_vars.to_json}'", @environment.ansible_host_id, "/root","360000", @url, @token)

    case response
      when Net::HTTPOK
        json = JSON.parse(response.body)
        track(json['id'])
      when Net::HTTPAccepted
        json = JSON.parse(response.body)
        track(json['id'])
      else
        Put.error response.body
        Put.error response.message
    end
  end
end

#status(response) ⇒ Object

Check request response status



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 174

def status(response)
  case response
    when Net::HTTPOK
      response = JSON.parse(response.body)
      if response['status'] == Configs::EnvironmentState::SUCCEEDED
        Put.success response['status']
        Put.info response['stdOut']
      elsif response['status'] == Configs::EnvironmentState::FAILED
        Put.error response['status']
        Put.info response['stdOut']
        Put.error response['stdErr']
      end
    else
     Put.error response.body
  end
end

#track(command_id) ⇒ Object

polls command log

"exitCode": 127,
"stdOut": "",
"stdErr": "bash: /root/get_unzip.sh: No such file or directory\n",
"status": "FAILED"



81
82
83
84
85
86
87
88
89
90
91
92
93
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
# File 'lib/vagrant-subutai/blueprint/ansible_controller.rb', line 81

def track(command_id)
  response = Rest::SubutaiConsole.command_log(@url, @token, command_id)

  case response
    when Net::HTTPOK
      @log = JSON.parse response.body
      @tmp = nil

      until @log['status'] == Configs::CommandState::FAILED    ||
            @log['status'] == Configs::CommandState::KILLED    ||
            @log['status'] == Configs::CommandState::TIMEOUT   ||
            @log['status'] == Configs::CommandState::SUCCEEDED

        res = Rest::SubutaiConsole.command_log(@url, @token, command_id)

        begin
          @log = JSON.parse res.body

          if @tmp.nil?
            Put.info @log['stdOut']
          else
            msg = @log['stdOut']

            if @tmp.length < msg.length
              msg = msg[(@tmp.length)..(msg.length-1)]
              Put.info msg
            end
          end

          @tmp = @log['stdOut']
        rescue JSON::ParserError => e
          Put.error e
        end

        sleep 5 # sleep 5 seconds
      end

      if @log['status'] == Configs::CommandState::SUCCEEDED
        Put.success @log['status']
      else
        Put.error @log['status']
        Put.error @log['stdErr']
      end
    else
      Put.error response.body
      Put.error response.message
  end
end