factor-connector-chef 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/factor/connector/chef.rb +127 -0
  3. metadata +128 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16a3d988e8c5fb00205765618e52c416b1377e0b
4
+ data.tar.gz: 2994d38c605354504a5b93d0c5b0d3e9d3d196ba
5
+ SHA512:
6
+ metadata.gz: 04d11de5d076cb46ac8232972246becd004bb9cda3cb3b3d5d0e3538d50b8192ef073f31795c0f6765f1fbcd2ef0d833f04fdeaa22aa32fd781a70926295d87c
7
+ data.tar.gz: 0b90fb7d72229ebdc5d9629f50883b785fe9210e67d9fee958337dccfd4ba599dd320caf10a18696c6cecbf989c2f7c9f06c3e57a7675b208511ecc3962c1335
@@ -0,0 +1,127 @@
1
+ require 'factor-connector-api'
2
+ require 'net/ssh'
3
+ require 'net/scp'
4
+ require 'tempfile'
5
+ require 'uri'
6
+
7
+ Factor::Connector.service 'chef' do
8
+ action 'bootstrap' do |params|
9
+ host_param = params['host']
10
+ private_key = params['private_key']
11
+ validation_key = params['validation_key']
12
+ runlist = params['runlist']
13
+ organization = params['organization']
14
+ node_name = params['name']
15
+
16
+ fail 'Host is required' unless host_param
17
+ fail 'Private Key (private_key) is required' unless private_key
18
+ fail 'Validation Key (validation_key) is required' unless validation_key
19
+ fail 'Organization (organization) is required' unless organization
20
+ fail 'Runlist (runlist) is required' unless runlist
21
+ fail 'Node Name (name) is required' unless node_name
22
+
23
+ validation_name = params['validation_name'] || "#{organization}-validator"
24
+
25
+
26
+ info 'Setting up the client.rb file'
27
+ client_rb = ""
28
+ client_rb << "log_location STDOUT\n"
29
+ client_rb << "chef_server_url \"https://api.opscode.com/organizations/#{organization}\"\n"
30
+ client_rb << "validation_client_name \"#{validation_name}\"\n"
31
+ client_rb << "node_name \"#{node_name}\"\n"
32
+
33
+
34
+
35
+ info 'Setting up private key'
36
+ begin
37
+ private_key_file = Tempfile.new('private')
38
+ private_key_file.write(private_key)
39
+ private_key_file.close
40
+ rescue
41
+ fail 'Failed to setup private key'
42
+ end
43
+
44
+ begin
45
+ uri = URI("ssh://#{host_param}")
46
+ host = uri.host
47
+ port = uri.port
48
+ user = uri.user
49
+ rescue => ex
50
+ fail "Couldn't parse input parameters", exception: ex
51
+ end
52
+
53
+ ssh_settings = { keys: [private_key_file.path] }
54
+ ssh_settings[:port] = port if port
55
+
56
+ fail 'User (user) is required in host address' unless user
57
+ fail 'Host variable must specific host address' unless host
58
+
59
+ setup_commands = [
60
+ 'curl -L https://www.opscode.com/chef/install.sh | sudo bash',
61
+ 'mkdir -p /etc/chef',
62
+ 'cd /etc/chef',
63
+ ]
64
+
65
+ run_commands = [
66
+ "chef-client --runlist #{runlist}"
67
+ ]
68
+
69
+ output = []
70
+ begin
71
+ Net::SSH.start(host, user, ssh_settings) do |ssh|
72
+
73
+ info 'Running setup commands'
74
+ setup_commands.each do |command|
75
+ info " running '#{command}'"
76
+ returned = ssh.exec!(command)
77
+ if returned && returned.is_a?(String)
78
+ lines = returned.split("\n")
79
+ lines.each do |line|
80
+ info " #{line}"
81
+ end
82
+ output = output + lines
83
+ end
84
+ end
85
+
86
+ info 'Uploading /etc/chef/validation.pem'
87
+ validation_string_io = StringIO.new(validation_key)
88
+ ssh.scp.upload!(validation_string_io, '/etc/chef/validation.pem')
89
+
90
+ info 'Uploading /etc/chef/client.rb'
91
+ client_string_io = StringIO.new(client_rb)
92
+ begin
93
+ ssh.scp.upload!(client_string_io, '/etc/chef/client.rb')
94
+ rescue
95
+ fail "Failed to upload /chef/client.rb"
96
+ end
97
+
98
+ info 'Running chef bootstrap commands'
99
+ run_commands.each do |command|
100
+ info " running '#{command}'"
101
+ returned = ssh.exec!(command)
102
+ if returned && returned.is_a?(String)
103
+ lines = returned.split("\n")
104
+ lines.each do |line|
105
+ info " #{line}"
106
+ end
107
+ output = output + lines
108
+ end
109
+ end
110
+ end
111
+ rescue Net::SSH::AuthenticationFailed
112
+ fail 'Authentication failure, check your SSH key, username, and host'
113
+ rescue => ex
114
+ fail "Couldn't connect to the server #{user}@#{host}:#{port || '22'}, please check credentials.", exception:ex
115
+ end
116
+
117
+ info 'Cleaning up.'
118
+ begin
119
+ private_key_file.unlink
120
+ validation_key_file.unlink
121
+ rescue
122
+ warn 'Failed to clean up, but no worries, work will go on.'
123
+ end
124
+
125
+ action_callback output: output
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factor-connector-chef
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Maciej Skierkowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ssh
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: net-scp
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: factor-connector-api
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.13
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.13
55
+ - !ruby/object:Gem::Dependency
56
+ name: codeclimate-test-reporter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 10.3.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 10.3.2
97
+ description:
98
+ email:
99
+ - maciej@factor.io
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - "./lib/factor/connector/chef.rb"
105
+ homepage: https://factor.io
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Factor.io Connector for Chef
128
+ test_files: []