Class: VagrantPlugins::ScriptRock::Provisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-scriptrock/provisioner.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine, config) ⇒ Provisioner



7
8
9
10
11
12
# File 'lib/vagrant-scriptrock/provisioner.rb', line 7

def initialize(machine, config)
  @debug = true#false
  @machine = machine
  @root_config = machine.config
  puts "provision initialize config" if @debug
end

Instance Method Details

#cleanupObject



119
120
121
122
# File 'lib/vagrant-scriptrock/provisioner.rb', line 119

def cleanup
  puts "provision cleanup" if @debug
  guardrail_delete
end

#configure(root_config) ⇒ Object



14
15
16
17
# File 'lib/vagrant-scriptrock/provisioner.rb', line 14

def configure(root_config)
  puts "provision configure" if @debug
  @root_config = root_config
end

#guardrail_auth_headersObject



23
24
25
# File 'lib/vagrant-scriptrock/provisioner.rb', line 23

def guardrail_auth_headers
  return { "Authorization" => "Token token=\"#{@root_config.scriptrock.api_key}#{@root_config.scriptrock.secret_key}\"" }
end

#guardrail_createObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-scriptrock/provisioner.rb', line 43

def guardrail_create
  url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes.json"
  response = HTTParty.post(url, :headers => guardrail_auth_headers, :body => {
      :node => {
        "name" => guardrail_name,
        "node_type" => "SV",
      },
    })
  responseJson = JSON.parse(response.body)
  if response.code == 201
    puts "ScriptRock: created new node, id #{responseJson["id"]} name #{guardrail_name}"
    return responseJson
  else
    throw "ScriptRock Guardrail create node error code #{response.code} body: #{response.body}"
  end
end

#guardrail_create_updateObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vagrant-scriptrock/provisioner.rb', line 94

def guardrail_create_update
  begin
    node = guardrail_lookup_and_show
    if node == nil
      node = guardrail_create
    end
    guardrail_update(node)
  rescue => e
    puts "Error contacting guardrail api: #{e.class}: #{e.message}"
  end
end

#guardrail_deleteObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vagrant-scriptrock/provisioner.rb', line 76

def guardrail_delete
  begin
    node = guardrail_lookup_and_show
    if node != nil
      url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/#{node["id"]}.json"
      response = HTTParty.delete(url, :headers => guardrail_auth_headers)
      if response.code == 204
        puts "ScriptRock: deleted node, id #{node["id"]} name #{guardrail_name}"
        return true
      else
        throw "ScriptRock Guardrail delete node error code #{response.code} body: #{response.body}"
      end      
    end
  rescue => e
    puts "Error contacting guardrail api: #{e.class}: #{e.message}"
  end
end

#guardrail_lookup_and_showObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagrant-scriptrock/provisioner.rb', line 27

def guardrail_lookup_and_show
  url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/lookup.json?name=#{CGI.escape(guardrail_name)}"
  response = HTTParty.get(url, :headers => guardrail_auth_headers)
  if response.code == 200
    responseJson = JSON.parse(response.body)
    url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/#{responseJson["node_id"]}.json"
    response = HTTParty.get(url, :headers => guardrail_auth_headers)
    responseJson = JSON.parse(response.body)
    if response.code == 200
      puts "ScriptRock: node already exists, id #{responseJson["id"]} name #{guardrail_name}"
      return responseJson
    end
  end
  return nil
end

#guardrail_nameObject



19
20
21
# File 'lib/vagrant-scriptrock/provisioner.rb', line 19

def guardrail_name
  return "vagrant #{@machine.name} #{@machine.id}"
end

#guardrail_update(node) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant-scriptrock/provisioner.rb', line 60

def guardrail_update(node)
  url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/#{node["id"]}.json"
  ssh_info = @machine.ssh_info
  node = {
    :medium_type => 3,
    :description => "#{@machine.name} (vagrant)",
    :medium_hostname => "#{@root_config.scriptrock.first_hop} ssh://#{ssh_info[:username]}@#{ssh_info[:host]}:#{ssh_info[:port]}".strip,
  }
  response = HTTParty.put(url, :headers => guardrail_auth_headers, :body => { :node => node })
  if response.code == 204
    return true
  else
    throw "ScriptRock Guardrail update node error code #{response.code} body: #{response.body}"
  end        
end

#provisionObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vagrant-scriptrock/provisioner.rb', line 106

def provision
  puts "provision provision" if @debug

  # insert the guardrail public key onto the target node if it is not already present
  @machine.communicate.tap do |comm|
    pk = @root_config.scriptrock.ssh_pubkey
    comm.execute("mkdir -p ~/.ssh && grep -q -s '#{pk}' ~/.ssh/authorized_keys || echo '#{pk}' >> ~/.ssh/authorized_keys")
  end

  # add this node to guardrail if not already present, then update to use the current credentials + forwarded port
  guardrail_create_update
end