Class: VagrantPlugins::ScriptRock::Provisioner

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

Defined Under Namespace

Classes: GuardrailPrivateKeyInstallError

Instance Method Summary collapse

Constructor Details

#initialize(machine, config) ⇒ Provisioner

Returns a new instance of Provisioner.



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

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

Instance Method Details

#cleanupObject



129
130
131
132
# File 'lib/vagrant-scriptrock/provisioner.rb', line 129

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

#configure(root_config) ⇒ Object



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

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

#guardrail_auth_headersObject



27
28
29
# File 'lib/vagrant-scriptrock/provisioner.rb', line 27

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

#guardrail_createObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-scriptrock/provisioner.rb', line 47

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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-scriptrock/provisioner.rb', line 100

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vagrant-scriptrock/provisioner.rb', line 80

def guardrail_delete
  begin
    node = guardrail_lookup_and_show
    if node == nil
      puts "ScriptRock: node with name '#{guardrail_name}' not found"
    else
      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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant-scriptrock/provisioner.rb', line 31

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



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

def guardrail_name
  return "#{@root_config.scriptrock.name_prefix} #{@machine.name}"
end

#guardrail_update(node) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant-scriptrock/provisioner.rb', line 64

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vagrant-scriptrock/provisioner.rb', line 112

def provision
  puts "provision provision" if @debug

  # insert the guardrail public key onto the target node if it is not already present
  puts "Checking for and possibly installing Guardrail public key in ~/.ssh/authorized_keys..."
  pk = @root_config.scriptrock.ssh_pubkey
  key_install_script = "mkdir -p ~/.ssh && " +
    "(grep -q -s '#{pk}' ~/.ssh/authorized_keys || echo '#{pk}' >> ~/.ssh/authorized_keys) && "+
    "grep -q -s '#{pk}' ~/.ssh/authorized_keys"
  @machine.communicate.tap do |comm|
    comm.execute(key_install_script, error_class: GuardrailPrivateKeyInstallError)
  end

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