lux 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 38ebce629f37858b431970695e06dd78bb7c1b04
4
+ data.tar.gz: 113e1a1c30136788ec9b334eb9fd7907bc5bb33e
5
+ SHA512:
6
+ metadata.gz: f582e29ebf238f9c80085ef8ceb9594ef9d3f5fa2acf384dec1f78a84884f13fbb65fa729c3ef91079f86196d53a6731c44e845b731edee6e8b30a1440815e8d
7
+ data.tar.gz: e978293de041075f563951bade0db7017ec6435f2bfb2533f8bee0ad731a2412722f4c0ccf2cc75dde31ea9c5b3c2957a1380a218b69fc2125f95b1590f60e3b
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.sw?
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lux.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nick Townsend
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Lux
2
+
3
+ Shining some light on setting up and running various Docker things.
4
+
5
+ Lux is both a command line tool (based on Thor) and a library of useful routines
6
+ that can be included in Rake tasks.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'lux'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install lux
23
+
24
+ ## Usage
25
+
26
+ Type: `lux help` to get started
27
+
28
+ ## Development
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( https://github.com/townsen/lux/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/lux ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lux"
5
+
6
+ Lux::App.start(ARGV)
data/lib/lux.rb ADDED
@@ -0,0 +1,160 @@
1
+ require "lux/version"
2
+ require 'pathname'
3
+ require 'shellwords'
4
+ require 'thor'
5
+
6
+ class Lux::App < Thor
7
+
8
+ # The Thor subclass containing useful tasks shared between the Lux
9
+ # standalone executable and Rake tasks.
10
+ #
11
+ desc "check", "Check the integrity of the Git repositories"
12
+ def check
13
+ repobranch = `git symbolic-ref -q HEAD`.chomp.gsub(%r{^.*/},'')
14
+ puts "Repository is on branch #{repobranch}"
15
+ `git fetch --recurse-submodules=on-demand`
16
+ nModuleErr = 0
17
+ nModuleWarn = 0
18
+ # Inspect the submodules currently checked out in turtle
19
+ submodules = Hash[`git submodule status --recursive`.split(/\n/).map do |s|
20
+ die "Bad submodule status #{s}" unless /^(?<flag>[-+U\s])(?<sha>[0-9a-f]{40})\s(?<path>\S+)(\s*\((?<ref>\S+)\))?$/ =~ s
21
+ case flag
22
+ when '-'
23
+ puts "Submodule at #{path} is not initialized!"
24
+ nModuleWarn += 1
25
+ when '+'
26
+ puts "Submodule at #{path} is not at correct commit!"
27
+ nModuleErr += 1
28
+ when 'U'
29
+ puts "Submodule at #{path} is conflicted!"
30
+ nModuleErr += 1
31
+ else
32
+ puts "Submodule at #{path} OK"
33
+ end
34
+ [path, [flag, sha, ref]]
35
+ end ]
36
+ die "There were #{nModuleErr} submodule errors and #{nModuleWarn} warnings" if nModuleErr > 0
37
+ # If the submodule status (above) was good, then we can ignore any submodule issues here
38
+ changes = `git status --porcelain`.split(/\n/).reject do |ch|
39
+ die "Bad status #{ch}" unless /^(?<x>.)(?<y>.)\s(?<path1>\S+)( -> (?<path2>\S+))?$/ =~ ch
40
+ submodules.include? path1
41
+ end
42
+ if changes.size > 0
43
+ die "Repository is not clean (#{changes.size} issues), use 'git status'"
44
+ else
45
+ puts "Repository is clean"
46
+ end
47
+ end
48
+
49
+ desc "start", "Run a Docker container with user/home mapping (default is 'base')"
50
+ method_option :image, type: :string, aliases: '-i', default: 'base', desc: 'Docker image'
51
+ method_option :env, type: :string, aliases: '-e', desc: 'Path to environment file'
52
+ method_option :name, type: :string, aliases: '-n', default: '<autogenerated>', desc: 'Docker container name'
53
+ def start
54
+ image = findimage options.image
55
+ puts "Starting #{image} container..."
56
+ me, setup_cmd = user_setup_cmd()
57
+ args = ["-v /home/#{me}:/home/#{me}"]
58
+ args << "--env-file=#{options.env}" if options.env
59
+ args << "--name=#{options.name}" unless options.name == '<autogenerated>'
60
+ cid = `docker run -dit #{args.join(' ')} #{image} /bin/bash`.strip
61
+ die "Container failed to start" unless cid =~ /^[a-z0-9]+$/
62
+ system "docker exec #{cid} bash -c #{setup_cmd.shellescape}"
63
+ puts "Type 'su [-] #{me}' then hit enter to attach to the container as yourself:"
64
+ Kernel.exec "docker attach #{cid}"
65
+ end
66
+
67
+ EXCLUDE_VARS = %w{
68
+ _ HOME PWD TMPDIR SSH_AUTH_SOCK SHLVL DISPLAY Apple_PubSub_Socket_Render SECURITYSESSIONID
69
+ XPC_SERVICE_NAME XPC_FLAGS __CF_USER_TEXT_ENCODING TERM_PROGRAM TERM_PROGRAM_VERSION TERM_SESSION_ID
70
+ }
71
+
72
+ desc "exec COMMAND", "Run a command inside a Docker container"
73
+ method_option :image, type: :string, aliases: '-i', default: 'base', desc: 'Docker image'
74
+ method_option :env, type: :string, aliases: '-e', desc: 'Path to environment file'
75
+ def exec(*command)
76
+ image = findimage options.image
77
+ me, setup_cmd = user_setup_cmd
78
+ die "You must be within your home directory!" unless relwd = Pathname.pwd.to_s.gsub!(/^#{ENV['HOME']}/,'~')
79
+ command.map!{|m| m.start_with?('/') ? Pathname.new(m).relative_path_from(Pathname.pwd) : m }
80
+ env = ENV.reject{|k,v| EXCLUDE_VARS.include? k or v =~/\s+/}.map{|k,v| "#{k}=#{v.shellescape}"}
81
+ env += IO.readlines(options.env).grep(/^(?!#)/).map(&:rstrip) if options.env
82
+ cmd = setup_cmd + "su - #{me} -c 'cd #{relwd}; env -i #{env.join(' ')} #{command.join(' ')}'"
83
+ args = ["-v /home/#{me}:/home/#{me}"]
84
+ system "docker run --rm #{args.join(' ')} #{image} /bin/bash -c #{cmd.shellescape}"
85
+ end
86
+
87
+ desc "clean", "Destroy all exited containers"
88
+ def clean
89
+ exited = `docker ps -q -f status=exited`.gsub! /\n/,' '
90
+ if exited and not exited.empty?
91
+ system "docker rm #{exited}"
92
+ else
93
+ puts "No exited containers"
94
+ end
95
+ end
96
+
97
+ desc "tidy", "Remove dangling Docker images"
98
+ def tidy
99
+ images = `docker images -f "dangling=true" -q`
100
+ if images.size > 0
101
+ system 'docker rmi $(docker images -f "dangling=true" -q)'
102
+ else
103
+ puts "No dangling images"
104
+ end
105
+ end
106
+
107
+ desc "clobber", "Destroy all containers (even if running!)"
108
+ def clobber
109
+ clean
110
+ running = `docker ps -q -f status=running`.gsub! /\n/,' '
111
+ if running and not running.empty?
112
+ system "docker rm -f #{running}"
113
+ else
114
+ puts "No running containers"
115
+ end
116
+ end
117
+
118
+ private
119
+
120
+ # Get the current list of images and make a guess at which one it is...
121
+ #
122
+ def findimage image
123
+ if image.count('/') == 0
124
+ local_images = `docker images`.strip.split(/\n/)[1..-1].map{|l| l.gsub!(/^(\S+)\s+(\S+).*/,'\1:\2')}.sort
125
+ matching_images = local_images.select{|l| l =~ %r[/#{image}] }
126
+ if matching_images.size > 0
127
+ if image.count(':') == 0
128
+ matching_image = matching_images.select{|l| l =~ /:latest$/ }.first
129
+ end
130
+ unless matching_image
131
+ matching_image = matching_images.first
132
+ end
133
+ else
134
+ matching_image = nil
135
+ end
136
+ image = matching_image ? matching_image : "lightside/"+image
137
+ end
138
+ return image
139
+ end
140
+
141
+ # Return two elements:
142
+ # - user name (defaults to current user), and
143
+ # - a bash script setup command
144
+ #
145
+ def user_setup_cmd user = `id -nu`.strip
146
+ [user, <<-COMMAND.gsub(/^\s*/,'').gsub(/\n/,' ; ')]
147
+ uid=$(echo $(stat -c %u:%g /home/#{user}) | cut -d: -f2)
148
+ useradd -M -u $uid -s #{ENV['SHELL']} #{user}
149
+ echo "#{user} ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/#{user}
150
+ COMMAND
151
+ end
152
+
153
+ def die msg, rc = 1
154
+ STDERR.puts msg
155
+ exit(rc)
156
+ end
157
+
158
+ end
159
+
160
+ # vim: ft=ruby sts=2 sw=2 ts=8
@@ -0,0 +1,3 @@
1
+ module Lux
2
+ VERSION = "0.1"
3
+ end
data/lux.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lux/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lux"
8
+ spec.version = Lux::VERSION
9
+ spec.authors = ["Nick Townsend"]
10
+ spec.email = ["nick.townsend@mac.com"]
11
+
12
+ spec.summary = %q{Handy utilities for working with Docker and Git}
13
+ spec.homepage = "https://github.com/townsen/lux"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'bin'
18
+ spec.executables << 'lux'
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = ">=2.0.0"
22
+
23
+ spec.add_runtime_dependency "thor"
24
+ spec.add_runtime_dependency "bundler", "~> 1.9"
25
+
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lux
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Nick Townsend
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - nick.townsend@mac.com
58
+ executables:
59
+ - lux
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/lux
71
+ - lib/lux.rb
72
+ - lib/lux/version.rb
73
+ - lux.gemspec
74
+ homepage: https://github.com/townsen/lux
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.0.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.5
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Handy utilities for working with Docker and Git
98
+ test_files: []