solokit 0.0.1 → 0.0.2

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.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/Gemfile.lock ADDED
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ solokit (0.0.2)
5
+ activesupport (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activesupport (3.0.9)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler
17
+ solokit!
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler'
2
+
3
+ Bundler::GemHelper.install_tasks
4
+
data/lib/chef.rb CHANGED
@@ -1,57 +1,59 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), 'ssh.rb'))
2
2
 
3
- class Chef
4
- def initialize(ip, name, env, debug_ssh, user = 'root')
5
- @ip, @name, @env = ip, name, env
6
- @ssh = SSH.new(ip, user, debug_ssh)
7
- end
3
+ module Solokit
4
+ class Chef
5
+ def initialize(ip, name, env, debug_ssh, user = 'root')
6
+ @ip, @name, @env = ip, name, env
7
+ @ssh = SSH.new(ip, user, debug_ssh)
8
+ end
8
9
 
9
- def install
10
- return true if installed?
11
- puts "#{@name} (#{@env}): Installing chef..."
12
- @ssh.run('export DEBIAN_FRONTEND=noninteractive; apt-get update && apt-get upgrade -y && apt-get install ruby ruby1.8-dev libopenssl-ruby wget rsync build-essential -y && wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz && tar xfz rubygems-1.3.7.tgz && cd rubygems-1.3.7 && ruby setup.rb && cd .. && rm -rf rubygems-1.3.7* && ln -s /usr/bin/gem1.8 /usr/bin/gem && gem install chef ohai --no-ri --no-rdoc')
13
- end
10
+ def install
11
+ return true if installed?
12
+ puts "#{@name} (#{@env}): Installing chef..."
13
+ @ssh.run('export DEBIAN_FRONTEND=noninteractive; apt-get update && apt-get upgrade -y && apt-get install ruby ruby1.8-dev libopenssl-ruby wget rsync build-essential -y && wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz && tar xfz rubygems-1.3.7.tgz && cd rubygems-1.3.7 && ruby setup.rb && cd .. && rm -rf rubygems-1.3.7* && ln -s /usr/bin/gem1.8 /usr/bin/gem && gem install chef ohai --no-ri --no-rdoc')
14
+ end
14
15
 
15
- def upload(root = "/")
16
- @ssh.run("rm -rf #{root}var/chef-solo", false) &&
17
- @ssh.run("rm -rf #{root}etc/chef", false) &&
18
- upload_files("cookbooks/upstream/", "#{root}var/chef-solo/upstream-cookbooks") &&
19
- upload_files("cookbooks/site/", "#{root}var/chef-solo/site-cookbooks") &&
20
- (File.exists?("envs/#{@env}/cookbooks") ?
21
- upload_files("envs/#{@env}/cookbooks/*", "#{root}var/chef-solo/site-cookbooks") :
22
- true) &&
23
- upload_files("chef/*", "#{root}etc/chef") &&
24
- upload_files("envs/#{@env}/chef/", "#{root}etc/chef")
25
- end
16
+ def upload(root = "/")
17
+ @ssh.run("rm -rf #{root}var/chef-solo", false) &&
18
+ @ssh.run("rm -rf #{root}etc/chef", false) &&
19
+ upload_files("cookbooks/upstream/", "#{root}var/chef-solo/upstream-cookbooks") &&
20
+ upload_files("cookbooks/site/", "#{root}var/chef-solo/site-cookbooks") &&
21
+ (File.exists?("envs/#{@env}/cookbooks") ?
22
+ upload_files("envs/#{@env}/cookbooks/*", "#{root}var/chef-solo/site-cookbooks") :
23
+ true) &&
24
+ upload_files("chef/*", "#{root}etc/chef") &&
25
+ upload_files("envs/#{@env}/chef/", "#{root}etc/chef")
26
+ end
26
27
 
27
- def run(debug = false, root = "/")
28
- puts "\n#{@name} (#{@env}): Running chef..."
29
- if debug
30
- @ssh.run("#{custom_ruby_path(root)} chef-solo -c #{root}etc/chef/solo.rb -j #{root}etc/chef/#{@name}.json -l debug", false)
31
- else
32
- @ssh.run("#{custom_ruby_path(root)} chef-solo -c #{root}etc/chef/solo.rb -j #{root}etc/chef/#{@name}.json", false)
28
+ def run(debug = false, root = "/")
29
+ puts "\n#{@name} (#{@env}): Running chef..."
30
+ if debug
31
+ @ssh.run("#{custom_ruby_path(root)} chef-solo -c #{root}etc/chef/solo.rb -j #{root}etc/chef/#{@name}.json -l debug", false)
32
+ else
33
+ @ssh.run("#{custom_ruby_path(root)} chef-solo -c #{root}etc/chef/solo.rb -j #{root}etc/chef/#{@name}.json", false)
34
+ end
33
35
  end
34
- end
35
36
 
36
- def self.provision!(env, host, config)
37
- chef = Chef.new(host, "server", env, config.debug_ssh)
38
- chef.install || raise("Chef install failed on #{env}")
39
- chef.upload || raise("Chef upload failed on #{env}")
40
- chef.run(config.debug_chef) || raise("Chef failed on #{env}")
41
- end
37
+ def self.provision!(env, host, config)
38
+ chef = Chef.new(host, "server", env, config.debug_ssh)
39
+ chef.install || raise("Chef install failed on #{env}")
40
+ chef.upload || raise("Chef upload failed on #{env}")
41
+ chef.run(config.debug_chef) || raise("Chef failed on #{env}")
42
+ end
42
43
 
43
- private
44
-
45
- def custom_ruby_path(root)
46
- "PATH=\"#{root}ruby/bin:/sbin:$PATH\""
47
- end
44
+ private
48
45
 
49
- def upload_files(from, to)
50
- @ssh.run("mkdir -p #{to}") &&
51
- @ssh.rsync(from, to, true)
52
- end
46
+ def custom_ruby_path(root)
47
+ "PATH=\"#{root}ruby/bin:/sbin:$PATH\""
48
+ end
53
49
 
54
- def installed?
55
- @ssh.run('gem list chef | grep chef')
50
+ def upload_files(from, to)
51
+ @ssh.run("mkdir -p #{to}") &&
52
+ @ssh.rsync(from, to, true)
53
+ end
54
+
55
+ def installed?
56
+ @ssh.run('gem list chef | grep chef')
57
+ end
56
58
  end
57
59
  end
data/lib/configuration.rb CHANGED
@@ -1,19 +1,20 @@
1
1
  require 'ostruct'
2
2
 
3
- class Configuration < OpenStruct
4
- def initialize(*envs)
5
- shared_config = YAML.load_file("config.yml")
6
-
7
- config = shared_config
8
- envs.each do |env|
9
- env_config_file = "envs/#{env}/config.yml"
3
+ module Solokit
4
+ class Configuration < OpenStruct
5
+ def initialize(*envs)
6
+ shared_config = YAML.load_file("config.yml")
10
7
 
11
- config = File.exists?(env_config_file) ?
12
- YAML.load_file(env_config_file).merge(config) :
13
- config
14
- end
8
+ config = shared_config
9
+ envs.each do |env|
10
+ env_config_file = "envs/#{env}/config.yml"
11
+
12
+ config = File.exists?(env_config_file) ?
13
+ YAML.load_file(env_config_file).merge(config) :
14
+ config
15
+ end
15
16
 
16
- super(config)
17
+ super(config)
18
+ end
17
19
  end
18
20
  end
19
-
@@ -1,3 +1,3 @@
1
1
  module Solokit
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/ssh.rb CHANGED
@@ -1,33 +1,34 @@
1
- class SSH
2
- def initialize(ip, user, debug)
3
- @ip, @user, @debug = ip, user, debug
4
- end
1
+ module Solokit
2
+ class SSH
3
+ def initialize(ip, user, debug)
4
+ @ip, @user, @debug = ip, user, debug
5
+ end
5
6
 
6
- def run(command, quiet = true)
7
- run_command("ssh #{ssh_opts} #{@user}@#{@ip} '#{command}' #{supress_output(quiet)}")
8
- end
7
+ def run(command, quiet = true)
8
+ run_command("ssh #{ssh_opts} #{@user}@#{@ip} '#{command}' #{supress_output(quiet)}")
9
+ end
9
10
 
10
- def rsync(source, target, quiet = false)
11
- run_command("rsync -e 'ssh #{ssh_opts}' -az #{source} #{@user}@#{@ip}:#{target} #{supress_output(quiet)}")
12
- end
11
+ def rsync(source, target, quiet = false)
12
+ run_command("rsync -e 'ssh #{ssh_opts}' -az #{source} #{@user}@#{@ip}:#{target} #{supress_output(quiet)}")
13
+ end
13
14
 
14
- private
15
+ private
15
16
 
16
- def supress_output(hide_stdout)
17
- if @debug
18
- ""
19
- else
20
- "#{hide_stdout ? '2> /dev/null 1> /dev/null' : '2> /dev/null'}"
17
+ def supress_output(hide_stdout)
18
+ if @debug
19
+ ""
20
+ else
21
+ "#{hide_stdout ? '2> /dev/null 1> /dev/null' : '2> /dev/null'}"
22
+ end
21
23
  end
22
- end
23
24
 
24
- def run_command(cmd)
25
- puts "SSH | #{cmd}" if @debug
26
- system(cmd)
27
- end
25
+ def run_command(cmd)
26
+ puts "SSH | #{cmd}" if @debug
27
+ system(cmd)
28
+ end
28
29
 
29
- def ssh_opts
30
- "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
30
+ def ssh_opts
31
+ "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
32
+ end
31
33
  end
32
34
  end
33
-
@@ -2,99 +2,100 @@ require 'yaml'
2
2
  require 'active_support'
3
3
  require 'active_support/json/encoding'
4
4
 
5
- class UserJsonGenerator
5
+ module Solokit
6
+ class UserJsonGenerator
6
7
 
7
- def initialize(key_path, env, opts = {})
8
- @key_path = key_path
8
+ def initialize(key_path, env, opts = {})
9
+ @key_path = key_path
9
10
 
10
- data = YAML.load_file("users.yml")
11
+ data = YAML.load_file("users.yml")
11
12
 
12
- @default_hash = data["default_hash"]
13
- @users = data["users"]
14
- @groups = data["groups"]
15
- @env = data["envs"][env]
16
- @opts = { :home => "/home" }.merge(opts)
17
- end
13
+ @default_hash = data["default_hash"]
14
+ @users = data["users"]
15
+ @groups = data["groups"]
16
+ @env = data["envs"][env]
17
+ @opts = { :home => "/home" }.merge(opts)
18
+ end
18
19
 
19
- def generate(target)
20
- if ENV['DEBUG_USER_JSON']
21
- require 'pp'
22
- ::PP.pp generate_json
23
- exit 0
20
+ def generate(target)
21
+ if ENV['DEBUG_USER_JSON']
22
+ require 'pp'
23
+ ::PP.pp generate_json
24
+ exit 0
25
+ end
26
+ File.open(target, 'w') { |f| f.write(generate_json) }
24
27
  end
25
- File.open(target, 'w') { |f| f.write(generate_json) }
26
- end
27
28
 
28
- def self.generate!(env, opts = {})
29
- UserJsonGenerator.new("public_keys", env, opts).generate("envs/#{env}/chef/roles/users.json")
30
- end
29
+ def self.generate!(env, opts = {})
30
+ UserJsonGenerator.new("public_keys", env, opts).generate("envs/#{env}/chef/roles/users.json")
31
+ end
31
32
 
32
- private
33
+ private
33
34
 
34
- def generate_json
35
- {
36
- :name => "Users",
37
- :chef_type => "role",
38
- :json_class => "Chef::Role",
39
- :override_attributes => {
35
+ def generate_json
36
+ {
37
+ :name => "Users",
38
+ :chef_type => "role",
39
+ :json_class => "Chef::Role",
40
+ :override_attributes => {
40
41
  :users => env_users.inject({}) { |h, user|
41
- h[user] = {
42
- :password => (@users[user] || {})["hash"] || @default_hash,
43
- :home => (user == 'root' ? '/root' : [ @opts[:home], user ].join('/')),
44
- :hidden_home => !!(@users[user] || {})["hidden_home"]
45
- }; h
46
- },
42
+ h[user] = {
43
+ :password => (@users[user] || {})["hash"] || @default_hash,
44
+ :home => (user == 'root' ? '/root' : [ @opts[:home], user ].join('/')),
45
+ :hidden_home => !!(@users[user] || {})["hidden_home"]
46
+ }; h
47
+ },
47
48
  :ssh_keys => env_users.inject({}) { |h, user|
48
- h[user] = get_keys(user); h
49
- },
49
+ h[user] = get_keys(user); h
50
+ },
50
51
  :authorization => {
51
- :sudo => { :users => env_sudo_users }
52
- }
52
+ :sudo => { :users => env_sudo_users }
53
+ }
53
54
  },
54
- :run_list => [ "recipe[users]", "recipe[sudo]" ]
55
- }.send(ENV['DEBUG_USER_JSON'] ? 'to_hash' : 'to_json')
56
- #.gsub(/\{/, "{\n").gsub(/\}/, "}\n")
57
- end
55
+ :run_list => [ "recipe[users]", "recipe[sudo]" ]
56
+ }.send(ENV['DEBUG_USER_JSON'] ? 'to_hash' : 'to_json')
57
+ #.gsub(/\{/, "{\n").gsub(/\}/, "}\n")
58
+ end
58
59
 
59
- def env_users
60
- resolve_users(@env["users"].split)
61
- end
60
+ def env_users
61
+ resolve_users(@env["users"].split)
62
+ end
62
63
 
63
- def env_sudo_users
64
- @env["sudo"] && resolve_users(@env["sudo"].split)
65
- end
64
+ def env_sudo_users
65
+ @env["sudo"] && resolve_users(@env["sudo"].split)
66
+ end
66
67
 
67
- def resolve_users(list)
68
- list.map { |item|
69
- if item.include?("group")
70
- fetch_group_users(item)
71
- else
72
- item
73
- end
74
- }.flatten
75
- end
68
+ def resolve_users(list)
69
+ list.map { |item|
70
+ if item.include?("group")
71
+ fetch_group_users(item)
72
+ else
73
+ item
74
+ end
75
+ }.flatten
76
+ end
76
77
 
77
- def get_keys(user)
78
- (@users[user] || {})["keys"].to_s.split.map do |key_name|
79
- if key_name.include?("group")
80
- keys_from_group(key_name)
81
- else
82
- key_file = "#{@key_path}/#{key_name}.pub"
83
- if File.exists?(key_file)
84
- File.read(key_file).chomp
78
+ def get_keys(user)
79
+ (@users[user] || {})["keys"].to_s.split.map do |key_name|
80
+ if key_name.include?("group")
81
+ keys_from_group(key_name)
85
82
  else
86
- raise "Could not find key file: #{key_file}."
83
+ key_file = "#{@key_path}/#{key_name}.pub"
84
+ if File.exists?(key_file)
85
+ File.read(key_file).chomp
86
+ else
87
+ raise "Could not find key file: #{key_file}."
88
+ end
87
89
  end
88
- end
89
- end.join("\n")
90
- end
90
+ end.join("\n")
91
+ end
91
92
 
92
- def fetch_group_users(name)
93
- @groups[name.split('/').last].split
94
- end
93
+ def fetch_group_users(name)
94
+ @groups[name.split('/').last].split
95
+ end
95
96
 
96
- def keys_from_group(name)
97
- @groups[name.split("/").last].split.map { |user| get_keys(user) }
97
+ def keys_from_group(name)
98
+ @groups[name.split("/").last].split.map { |user| get_keys(user) }
99
+ end
98
100
  end
99
101
  end
100
-
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solokit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Joakim Kolsj\xC3\xB6"
@@ -58,10 +58,13 @@ extensions: []
58
58
  extra_rdoc_files: []
59
59
 
60
60
  files:
61
+ - .gitignore
61
62
  - .rvmrc
62
63
  - Gemfile
64
+ - Gemfile.lock
63
65
  - LICENCE
64
66
  - README
67
+ - Rakefile
65
68
  - lib/chef.rb
66
69
  - lib/configuration.rb
67
70
  - lib/solokit.rb