cosmo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ servers.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cosmo.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 =
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,59 @@
1
+ # Cosmo
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cosmo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cosmo
18
+
19
+ ## Usage
20
+ Create a servers.yml in file `.` in the following format:
21
+
22
+ ```yaml
23
+ group_name:
24
+ host_name:
25
+ - account1
26
+ - account2
27
+ ```
28
+
29
+ For example:
30
+
31
+ ```yaml
32
+ workers:
33
+ xyz.com:
34
+ - bill
35
+ abc.com:
36
+ - jeff
37
+ ```
38
+
39
+ The create a ruby script:
40
+
41
+ ```ruby
42
+ require 'cosmo'
43
+
44
+ job "install ruby 1.9.2" do
45
+ on_group "workers" do |g|
46
+ commands "rvm install 1.9.2" do |c|
47
+ g.run c
48
+ end
49
+ end
50
+ end
51
+ ```
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/cosmo.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/cosmo/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["="]
6
+ gem.email = ["thom.mulvaney@gmail.com"]
7
+ gem.description = %q{Makes running things on multiple machines easy}
8
+ gem.summary = %q{Makes running things on multiple machines easy}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "cosmo"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Cosmo::VERSION
17
+ gem.add_runtime_dependency "colors"
18
+ gem.add_runtime_dependency "trollop"
19
+ gem.add_runtime_dependency "net-ssh"
20
+ end
@@ -0,0 +1,94 @@
1
+ class Account
2
+ attr_reader :machine, :user
3
+
4
+ def initialize machine, user
5
+ @machine = machine
6
+ @user = user
7
+ end
8
+
9
+ def ssh_exec!(ssh, command)
10
+ stdout_data = ""
11
+ stderr_data = ""
12
+ exit_code = nil
13
+ exit_signal = nil
14
+ ssh.open_channel do |channel|
15
+ channel.exec(command) do |ch, success|
16
+ unless success
17
+ abort "FAILED: couldn't execute command (ssh.channel.exec)"
18
+ end
19
+ channel.on_data do |ch,data|
20
+ stdout_data+=data
21
+ end
22
+
23
+ channel.on_extended_data do |ch,type,data|
24
+ stderr_data+=data
25
+ end
26
+
27
+ channel.on_request("exit-status") do |ch,data|
28
+ exit_code = data.read_long
29
+ end
30
+
31
+ channel.on_request("exit-signal") do |ch, data|
32
+ exit_signal = data.read_long
33
+ end
34
+ end
35
+ end
36
+ ssh.loop
37
+ [stdout_data, stderr_data, exit_code, exit_signal]
38
+ end
39
+
40
+ def run c
41
+ s = " #{self}:".ljust(40) + "#{c} "
42
+ unless Cosmo::Cosmo.dry?
43
+ Net::SSH.start(self.machine, self.user) do |ssh|
44
+ e = ssh_exec! ssh, c
45
+ if e[2] == 0
46
+ puts s.green
47
+ else
48
+ puts s.red
49
+ end
50
+ end
51
+ else
52
+ puts s.blue
53
+ end
54
+ end
55
+
56
+ def pwd
57
+ full = ENV['PWD']
58
+ home = ENV['HOME']
59
+ full.sub(home, "~")
60
+ end
61
+
62
+ def cp options = {:files => [], :to => []}
63
+ puts "please supply a list of servers" if options[:to].empty?
64
+ puts "please supply a list of files" if options[:files].empty?
65
+ unless options[:files].empty? and options[:to].empty?
66
+ options[:files].each do |file|
67
+ options[:to].each do |server|
68
+ unless server.machine == 'localhost'
69
+ server.run "mkdir -p #{File.dirname(file)}"
70
+ self.run "scp '#{self.user}@#{self.machine}:#{file}' #{server.user}@#{server.machine}:#{pwd}"
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def sync options = {:to => []}
78
+ options[:to].each do |remote|
79
+ remote.run "mkdir -p #{pwd}"
80
+ self.run "rsync -a --exclude '*.xyz' #{pwd} #{remote}:#{File.dirname(pwd)}"
81
+ end
82
+ end
83
+
84
+ def local?
85
+ self.machine == "localhost"
86
+ end
87
+
88
+ def to_s
89
+ "#{self.user}@#{self.machine}"
90
+ end
91
+
92
+ end
93
+
94
+
@@ -0,0 +1,44 @@
1
+ class Cosmo::Cosmo
2
+ @@dry = false
3
+ def initialize
4
+ @groups = {}
5
+ @dry =false
6
+ file = nil
7
+ begin
8
+ file = File.open("servers.yml")
9
+ rescue
10
+ puts "Please create a servers.yml file"
11
+ file = nil
12
+ end
13
+ unless file.nil?
14
+ config = YAML.load(file.read)
15
+ config.each do |group,servers|
16
+ accounts = []
17
+ servers.each do |server, users|
18
+ users.each do |user|
19
+ accounts.push Account.new server, user
20
+ end
21
+ end
22
+ grouped = Group.new group, accounts
23
+ @groups[group] = grouped
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.groups
29
+ self.new.groups
30
+ end
31
+
32
+ def self.dry?
33
+ @@dry
34
+ end
35
+
36
+ def self.dry=(bool)
37
+ @@dry = bool
38
+ end
39
+
40
+ def groups
41
+ @groups
42
+ end
43
+
44
+ end
@@ -0,0 +1,54 @@
1
+ class Group
2
+ def initialize name, accounts
3
+ @name = name
4
+ @accounts = accounts
5
+ end
6
+
7
+ def run *commands
8
+ @accounts.each do |account|
9
+ commands.each do |command|
10
+ account.run command
11
+ end
12
+ end
13
+ end
14
+
15
+ def dry_run *commands
16
+ @accounts.each do |account|
17
+ commands.each do |command|
18
+ account.dry_run command
19
+ end
20
+ end
21
+ end
22
+
23
+ def machines
24
+ @accounts.map {|a| a.machine}.uniq
25
+ end
26
+
27
+ def accounts
28
+ @accounts
29
+ end
30
+
31
+ def local
32
+ locals = []
33
+ @accounts.each {|m| locals.push m if m.local?}
34
+ locals.first
35
+ end
36
+
37
+ def remotes
38
+ remotes = []
39
+ @accounts.each {|m| remotes.push m unless m.local?}
40
+ remotes
41
+ end
42
+
43
+ def cp options = {:files => [], :to => []}
44
+ @accounts.each do |account|
45
+ account.cp options
46
+ end
47
+ end
48
+
49
+ def run *commands
50
+ @accounts.each {|a| a.run *commands}
51
+ end
52
+ end
53
+
54
+
@@ -0,0 +1,3 @@
1
+ module Cosmo
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cosmo.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "yaml"
2
+ require "net/ssh"
3
+ require "cosmo/version"
4
+ require "cosmo/account"
5
+ require "cosmo/group"
6
+ require "cosmo/cosmo"
7
+ require "colored"
8
+ require "trollop"
9
+
10
+ module Cosmo
11
+ opts = Trollop::options do
12
+ opt :dry, "dry run"
13
+ end
14
+
15
+ if opts[:dry]
16
+ puts "This is a dry run!".blue
17
+ Cosmo.dry = true
18
+ end
19
+
20
+ def job name, &job
21
+ s = "Job: #{name}"
22
+ puts s.blue
23
+ job.call
24
+ end
25
+
26
+ def on_group name, &group
27
+ puts " => On worker group"
28
+ group[Cosmo.groups[name]]
29
+ puts ""
30
+ end
31
+
32
+ def commands *comms, &block
33
+ count = comms.count
34
+ comms.each_with_index do |c,i|
35
+ puts " => Command (#{i+1}/#{count}): #{c}"
36
+ yield c
37
+ end
38
+ end
39
+
40
+ def pwd
41
+ full = ENV['PWD']
42
+ home = ENV['HOME']
43
+ full.sub(home, "~")
44
+ end
45
+ end
46
+
47
+ include Cosmo
48
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cosmo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - =
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colors
16
+ requirement: &70145084432000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70145084432000
25
+ - !ruby/object:Gem::Dependency
26
+ name: trollop
27
+ requirement: &70145084431580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70145084431580
36
+ - !ruby/object:Gem::Dependency
37
+ name: net-ssh
38
+ requirement: &70145084431160 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70145084431160
47
+ description: Makes running things on multiple machines easy
48
+ email:
49
+ - thom.mulvaney@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - cosmo.gemspec
60
+ - lib/cosmo.rb
61
+ - lib/cosmo/account.rb
62
+ - lib/cosmo/cosmo.rb
63
+ - lib/cosmo/group.rb
64
+ - lib/cosmo/version.rb
65
+ homepage: ''
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.15
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Makes running things on multiple machines easy
89
+ test_files: []