vagrant-scriptrock 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6426244896b029fec557201523eef0dd47259345
4
+ data.tar.gz: 7ee812b8f4a6b06bc12c082a655ac0dc75dcf3d6
5
+ SHA512:
6
+ metadata.gz: 19f803cd69f5fe11c5c73d2802fdff3ac535b06b84f1793c00ec006f6db0b12b2842a401dbb1584fd9756de3f11015d9881bd8191840688db6d5986eff0cdde2
7
+ data.tar.gz: 8fd8e862f6f28184eb7f58277c6c2e81ec906665e7e28766a42b644d6cf53b4967bc154e61851dbeaea06d8e488b6f66848b28995c1b911dfec31cf2e99d381c
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ .DS_Store
2
+
3
+ .bundle
4
+ pkg/*
5
+ Gemfile.lock
6
+ Gemfile.dev
7
+ Gemfile.dev.lock
8
+
9
+ coverage
10
+
11
+ .rspec
12
+
13
+ .vagrant
14
+ .vagrant.d/*
15
+ Vagrantfile
16
+
17
+ *~
18
+ .*~
19
+ .*.swo
20
+ .*.swp
21
+
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :development do
4
+ gem "vagrant", git: "https://github.com/mitchellh/vagrant.git"
5
+ end
6
+
7
+ group :plugins do
8
+ gem "vagrant-scriptrock", path: "."
9
+ end
10
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Mark Sheahan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Vagrant::Scriptrock
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vagrant-scriptrock'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vagrant-scriptrock
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/vagrant-scriptrock/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,116 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'yaml'
4
+
5
+ module VagrantPlugins
6
+ module ScriptRock
7
+ class Config < Vagrant.plugin(2, :config)
8
+ attr_accessor :scriptrock_yml_path
9
+ attr_accessor :first_hop
10
+ attr_accessor :api_key
11
+ attr_accessor :secret_key
12
+ attr_accessor :connect_url
13
+ attr_accessor :ssh_pubkey
14
+
15
+ def initialize
16
+ @debug = true#false
17
+ puts "config initialize" if @debug
18
+ @scriptrock_yml_path = "~/.scriptrock/scriptrock.yml"
19
+ @first_hop = UNSET_VALUE
20
+ @api_key = UNSET_VALUE
21
+ @secret_key = UNSET_VALUE
22
+ @connect_url = UNSET_VALUE
23
+ @ssh_pubkey = UNSET_VALUE
24
+ end
25
+
26
+ def load_vars_from_yml
27
+ if unset(@api_key) || unset(@secret_key) || unset(@connect_url)
28
+ if unset(@scriptrock_yml_path)
29
+ puts "ScriptRock yml config path un-set, not loading values from yml"
30
+ elsif !File.exist?(File.expand_path(@scriptrock_yml_path))
31
+ puts "ScriptRock yml file '#{@scriptrock_yml_path}' doesn't exist, not loading values from yml"
32
+ else
33
+ yml = YAML.load(File.read(File.expand_path(@scriptrock_yml_path)))
34
+ puts yml if @debug
35
+ if unset(@api_key)
36
+ @api_key = yml["api_key"]
37
+ end
38
+ if unset(@secret_key)
39
+ @secret_key = yml["secret_key"]
40
+ end
41
+ if unset(@connect_url)
42
+ @connect_url = yml["connect_url"]
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def get_ssh_pubkey
49
+ if @ssh_pubkey != UNSET_VALUE
50
+ return @ssh_pubkey
51
+ end
52
+
53
+ load_vars_from_yml()
54
+ begin
55
+ puts "ScriptRock API connect_url #{@connect_url}" if @debug
56
+ response = HTTParty.get(
57
+ "#{@connect_url}/api/v1/users/ssh_key.json",
58
+ :headers => {
59
+ "Authorization" => "Token token=\"#{@api_key}\""
60
+ })
61
+ if response.code == 200
62
+ h = JSON.parse(response.body)
63
+ @ssh_pubkey = h["public_key"]
64
+ else
65
+ puts "ScriptRock get_ssh_pubkey error code = #{response.code}"
66
+ puts "ScriptRock get_ssh_pubkey error body = #{response.body}"
67
+ end
68
+ rescue => e
69
+ puts "ScriptRock get_ssh_pubkey error = #{e.class}: #{e.message}"
70
+ end
71
+ return @ssh_pubkey
72
+ end
73
+
74
+ def finalize!
75
+ puts "finalize!" if @debug
76
+ get_ssh_pubkey()
77
+ if unset(@first_hop)
78
+ @first_hop = ""
79
+ end
80
+ end
81
+
82
+ def unset(v)
83
+ return v == UNSET_VALUE || v == nil || v == ""
84
+ end
85
+
86
+ def dump
87
+ puts "config dump"
88
+ puts "yml_path #{@scriptrock_yml_path}"
89
+ puts "api_key #{@api_key}"
90
+ puts "connect_url #{@connect_url}"
91
+ puts "ssh_pubkey #{@ssh_pubkey}"
92
+ end
93
+
94
+ def validate(machine)
95
+ puts "config validate" if @debug
96
+ dump if @debug
97
+
98
+ errors = _detected_errors
99
+ if unset(@api_key)
100
+ errors << "ScriptRock Guardrail api_key is not set"
101
+ end
102
+ if unset(@secret_key)
103
+ errors << "ScriptRock Guardrail secret_key is not set"
104
+ end
105
+ if unset(@connect_url)
106
+ errors << "ScriptRock Guardrail connect_url is not set"
107
+ end
108
+ if unset(@ssh_pubkey)
109
+ errors << "ScriptRock Guardrail ssh public key is not set"
110
+ end
111
+
112
+ { "errors" => errors }
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,30 @@
1
+ require "vagrant"
2
+
3
+ # This is a sanity check to make sure no one is attempting to install
4
+ # this into an early Vagrant version.
5
+ if Vagrant::VERSION < "1.2.0"
6
+ raise "The Vagrant ScriptRock plugin is only compatible with Vagrant 1.2+"
7
+ end
8
+
9
+ module VagrantPlugins
10
+ module ScriptRock
11
+ class Plugin < Vagrant.plugin("2")
12
+ name "ScriptRock"
13
+ description <<-DESC
14
+ This plugin will install a Guardrail public key ~/.ssh/authorized_keys
15
+ on the instantiated vm, register the new node in the target Guardrail site,
16
+ and delete the node from the Guardrail when the vm is destroyed.
17
+ DESC
18
+
19
+ config(:scriptrock) do
20
+ require_relative "config"
21
+ Config
22
+ end
23
+
24
+ provisioner(:scriptrock) do
25
+ require_relative "provisioner"
26
+ Provisioner
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,125 @@
1
+ require 'cgi'
2
+ require 'httparty'
3
+
4
+ module VagrantPlugins
5
+ module ScriptRock
6
+ class Provisioner < Vagrant.plugin("2", :provisioner)
7
+ def initialize(machine, config)
8
+ @debug = true#false
9
+ @machine = machine
10
+ @root_config = machine.config
11
+ puts "provision initialize config" if @debug
12
+ end
13
+
14
+ def configure(root_config)
15
+ puts "provision configure" if @debug
16
+ @root_config = root_config
17
+ end
18
+
19
+ def guardrail_name
20
+ return "vagrant #{@machine.name} #{@machine.id}"
21
+ end
22
+
23
+ def guardrail_auth_headers
24
+ return { "Authorization" => "Token token=\"#{@root_config.scriptrock.api_key}#{@root_config.scriptrock.secret_key}\"" }
25
+ end
26
+
27
+ def guardrail_lookup_and_show
28
+ url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/lookup.json?name=#{CGI.escape(guardrail_name)}"
29
+ response = HTTParty.get(url, :headers => guardrail_auth_headers)
30
+ if response.code == 200
31
+ responseJson = JSON.parse(response.body)
32
+ url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/#{responseJson["node_id"]}.json"
33
+ response = HTTParty.get(url, :headers => guardrail_auth_headers)
34
+ responseJson = JSON.parse(response.body)
35
+ if response.code == 200
36
+ puts "ScriptRock: node already exists, id #{responseJson["id"]} name #{guardrail_name}"
37
+ return responseJson
38
+ end
39
+ end
40
+ return nil
41
+ end
42
+
43
+ def guardrail_create
44
+ url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes.json"
45
+ response = HTTParty.post(url, :headers => guardrail_auth_headers, :body => {
46
+ :node => {
47
+ "name" => guardrail_name,
48
+ "node_type" => "SV",
49
+ },
50
+ })
51
+ responseJson = JSON.parse(response.body)
52
+ if response.code == 201
53
+ puts "ScriptRock: created new node, id #{responseJson["id"]} name #{guardrail_name}"
54
+ return responseJson
55
+ else
56
+ throw "ScriptRock Guardrail create node error code #{response.code} body: #{response.body}"
57
+ end
58
+ end
59
+
60
+ def guardrail_update(node)
61
+ url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/#{node["id"]}.json"
62
+ ssh_info = @machine.ssh_info
63
+ node = {
64
+ :medium_type => 3,
65
+ :description => "#{@machine.name} (vagrant)",
66
+ :medium_hostname => "#{@root_config.scriptrock.first_hop} ssh://#{ssh_info[:username]}@#{ssh_info[:host]}:#{ssh_info[:port]}".strip,
67
+ }
68
+ response = HTTParty.put(url, :headers => guardrail_auth_headers, :body => { :node => node })
69
+ if response.code == 204
70
+ return true
71
+ else
72
+ throw "ScriptRock Guardrail update node error code #{response.code} body: #{response.body}"
73
+ end
74
+ end
75
+
76
+ def guardrail_delete
77
+ begin
78
+ node = guardrail_lookup_and_show
79
+ if node != nil
80
+ url = "#{@root_config.scriptrock.connect_url}/api/v1/nodes/#{node["id"]}.json"
81
+ response = HTTParty.delete(url, :headers => guardrail_auth_headers)
82
+ if response.code == 204
83
+ puts "ScriptRock: deleted node, id #{node["id"]} name #{guardrail_name}"
84
+ return true
85
+ else
86
+ throw "ScriptRock Guardrail delete node error code #{response.code} body: #{response.body}"
87
+ end
88
+ end
89
+ rescue => e
90
+ puts "Error contacting guardrail api: #{e.class}: #{e.message}"
91
+ end
92
+ end
93
+
94
+ def guardrail_create_update
95
+ begin
96
+ node = guardrail_lookup_and_show
97
+ if node == nil
98
+ node = guardrail_create
99
+ end
100
+ guardrail_update(node)
101
+ rescue => e
102
+ puts "Error contacting guardrail api: #{e.class}: #{e.message}"
103
+ end
104
+ end
105
+
106
+ def provision
107
+ puts "provision provision" if @debug
108
+
109
+ # insert the guardrail public key onto the target node if it is not already present
110
+ @machine.communicate.tap do |comm|
111
+ pk = @root_config.scriptrock.ssh_pubkey
112
+ comm.execute("mkdir -p ~/.ssh && grep -q -s '#{pk}' ~/.ssh/authorized_keys || echo '#{pk}' >> ~/.ssh/authorized_keys")
113
+ end
114
+
115
+ # add this node to guardrail if not already present, then update to use the current credentials + forwarded port
116
+ guardrail_create_update
117
+ end
118
+
119
+ def cleanup
120
+ puts "provision cleanup" if @debug
121
+ guardrail_delete
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,5 @@
1
+ module VagrantPlugins
2
+ module ScriptRock
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ require "pathname"
2
+ require "vagrant-scriptrock/plugin"
3
+
4
+ module VagrantPlugins
5
+ module ScriptRock
6
+ lib_path = Pathname.new(File.expand_path("../vagrant-scriptrock", __FILE__))
7
+ autoload :Config, lib_path.join("config")
8
+ autoload :Provisioner, lib_path.join("provisioner")
9
+
10
+ # This returns the path to the source of this plugin.
11
+ #
12
+ # @return [Pathname]
13
+ def self.source_root
14
+ @source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "vagrant-scriptrock/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-scriptrock"
8
+ spec.version = VagrantPlugins::ScriptRock::VERSION
9
+ spec.authors = ["Mark Sheahan"]
10
+ spec.email = ["mark.sheahan@scriptrock.com"]
11
+ spec.homepage = "https://github.com/ScriptRock/vagrant-scriptrock"
12
+ spec.summary = "Vagrant plugin for ScriptRock Guardrail node registry/deletion when Vagrant VMs are provisioned/destroyed"
13
+ spec.description = '''This plugin will install a Guardrail public key ~/.ssh/authorized_keys
14
+ on the instantiated vm, register the VM as a new node on the target Guardrail site,
15
+ and delete the Guardrail node when the VM is destroyed.'''
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
26
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-scriptrock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Sheahan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |-
42
+ This plugin will install a Guardrail public key ~/.ssh/authorized_keys
43
+ on the instantiated vm, register the VM as a new node on the target Guardrail site,
44
+ and delete the Guardrail node when the VM is destroyed.
45
+ email:
46
+ - mark.sheahan@scriptrock.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".gitignore"
52
+ - CHANGELOG.md
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - lib/vagrant-scriptrock.rb
58
+ - lib/vagrant-scriptrock/config.rb
59
+ - lib/vagrant-scriptrock/plugin.rb
60
+ - lib/vagrant-scriptrock/provisioner.rb
61
+ - lib/vagrant-scriptrock/version.rb
62
+ - vagrant-scriptrock.gemspec
63
+ homepage: https://github.com/ScriptRock/vagrant-scriptrock
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.2.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Vagrant plugin for ScriptRock Guardrail node registry/deletion when Vagrant
87
+ VMs are provisioned/destroyed
88
+ test_files: []