odania_ops 0.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: f42edd75a33b30dc4e03ae007b144552d61a6ec0
4
+ data.tar.gz: b65df0c2568b7f53e79e3c8b0fecf524e82507f2
5
+ SHA512:
6
+ metadata.gz: 20f456049862905d6431e812e94b8cba282618a757a3c0b1073cfcfb6efb46dd8372125b3bb0b5afea3df7ed2e92f814ddddbeddeeda13cdd10dc11cb9f32259
7
+ data.tar.gz: 0e3b14e76ef8874a15313d4369369c57b4673a1c947b85ca0c7987d9e747c9270f051c456d2e2d20cf9e85b978820b135e3862cbf52447957755fc16089ee51c
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in odania.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ odania_ops (0.0.1)
5
+ deep_merge
6
+ docker-api
7
+ httparty
8
+ thor
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ deep_merge (1.0.1)
14
+ docker-api (1.24.1)
15
+ excon (>= 0.38.0)
16
+ json
17
+ excon (0.45.4)
18
+ httparty (0.13.7)
19
+ json (~> 1.8)
20
+ multi_xml (>= 0.5.2)
21
+ json (1.8.3)
22
+ multi_xml (0.5.5)
23
+ thor (0.19.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ odania_ops!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mike Petersen Odania-IT
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # ops-tools
2
+
3
+ Helper scripts for managing the servers.
4
+
5
+ ## Configuration
6
+
7
+ The configuration file ops-config.yml is searched either under /etc or the directory of the ops installation and all paths above.
8
+
9
+ You can use the command
10
+ ```
11
+ ops config init
12
+ ```
13
+ to create or update a configuration with all options.
14
+
15
+ Example:
16
+ ```
17
+ docker:
18
+ email: your-mail@example.com
19
+ user: my-user
20
+ password: my-password
21
+ url: https://my-docker-registry
22
+ ```
23
+
24
+ ## Docker
25
+
26
+ ### build
27
+
28
+ This command builds tags and pushes an image to a docker registry.
29
+
30
+ Example:
31
+ ```
32
+ bundle exec ops docker build ~/workspace/docker/docker-jenkins-odania docker-jenkins-odania
33
+ ```
34
+
35
+ This will do the following steps:
36
+ 1. Login to the registry
37
+ 2. Get the highest version number from the registry
38
+ 3. Detect the base image (in this case odaniait/docker-jenkins:latest) and pull it. To make sure it is up to date.
39
+ 4. Build the docker image in the folder
40
+ 5. Tag the image with the version vBUILD_NUMBER and latest
41
+ 6. Push the image
42
+
43
+ You can additionally add the version number as a last parameter, e.g.
44
+ ```
45
+ bundle exec ops docker build ~/workspace/docker/docker-jenkins-odania docker-jenkins-odania 10
46
+ ```
47
+ This will build and push to v10 and latest.
48
+
49
+ #### Version numbers
50
+
51
+ Version numbers are expected to be in tags like v1 (vNUMBER in general).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc 'Run tests'
9
+ task :default => :test
data/bin/odania-ops ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'odania_ops'
3
+
4
+ OdaniaOps::Cli::Application.start(ARGV)
data/bin/ops ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'odania_ops'
3
+
4
+ OdaniaOps::Cli::Application.start(ARGV)
@@ -0,0 +1,28 @@
1
+ module OdaniaOps
2
+ module Cli
3
+ class Config < Thor
4
+ desc 'init <folder>', 'Initializes a configuration file under <folder>. If not set "/etc" is used'
5
+
6
+ def init(folder='/etc')
7
+ config_file = File.expand_path 'ops-config.yml', folder
8
+ current_config = default_config.deeper_merge! $config
9
+
10
+ $logger.info "Writting new configuration to #{config_file}"
11
+ File.open(config_file, 'w') {|f| f.write current_config.to_yaml }
12
+ end
13
+
14
+ private
15
+
16
+ def default_config
17
+ {
18
+ 'docker' => {
19
+ 'email' => '',
20
+ 'user' => '',
21
+ 'password' => '',
22
+ 'url' => ''
23
+ }
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ module OdaniaOps
2
+ module Cli
3
+ class Docker < Thor
4
+ desc 'build <folder> <image_name> <version_number>', 'Builds the docker image under <folder>'
5
+ def build(folder, image_name, version_number=nil)
6
+ OdaniaOps::Helper::Docker.login
7
+
8
+ build_number = version_number.nil? ? get_highest_build_number(image_name) + 1 : version_number.to_i
9
+ build_tag = "v#{build_number}"
10
+ $logger.info "Building version #{build_tag}"
11
+
12
+ base_image = get_base_image(folder)
13
+ $logger.info "Pulling base image #{base_image}"
14
+ OdaniaOps::Helper::Shell.execute("docker pull #{base_image}")
15
+
16
+ $logger.info "Building #{image_name}"
17
+ OdaniaOps::Helper::Shell.execute("cd #{folder} && docker build -t #{image_name}:#{build_tag} .")
18
+
19
+ $logger.info "Tagging #{build_tag} as latest"
20
+ OdaniaOps::Helper::Docker.remote_tag "#{image_name}:#{build_tag}"
21
+ OdaniaOps::Helper::Docker.remote_tag "#{image_name}:#{build_tag}", "#{image_name}:latest", true
22
+
23
+ $logger.info "Pushing #{build_tag}"
24
+ OdaniaOps::Helper::Docker.push image_name
25
+ end
26
+
27
+ private
28
+
29
+ def get_highest_build_number(image_name)
30
+ build_numbers = OdaniaOps::Helper::Docker.image_tags(image_name).map { |tag| tag.gsub('v', '').to_i }.sort
31
+ puts build_numbers.inspect
32
+ return 0 if build_numbers.empty?
33
+ build_numbers.pop
34
+ end
35
+
36
+ def get_base_image(folder)
37
+ docker_file = File.expand_path 'Dockerfile', folder
38
+ contents = File.read(docker_file)
39
+ contents.split("\n").each do |line|
40
+ return line.gsub('FROM', '').strip if line.start_with? 'FROM'
41
+ end
42
+
43
+ raise "No base image detected in #{docker_file}"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,37 @@
1
+ module OdaniaOps
2
+ module Helper
3
+ module Config
4
+ class << self
5
+ def load_config(folder)
6
+ config_file = nil
7
+ begin
8
+ config_file = retrieve_config_folder '/etc'
9
+ rescue RuntimeError
10
+ config_file = retrieve_config_folder folder
11
+ end
12
+
13
+ $logger.debug "Loading config file #{config_file}"
14
+ $config = YAML.load_file(config_file)
15
+ $logger.debug $config.inspect
16
+ end
17
+
18
+ def retrieve_config_folder(start_folder)
19
+ folder = start_folder
20
+ loop do
21
+ break unless File.directory?(folder)
22
+
23
+ config_file = File.expand_path('ops-config.yml', folder)
24
+ return config_file if File.exists? config_file
25
+
26
+ next_folder = File.expand_path('..', folder)
27
+
28
+ break if next_folder.eql?(folder)
29
+ folder = next_folder
30
+ end
31
+
32
+ raise "No configuration found! Looking in #{start_folder} and above."
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,56 @@
1
+ module OdaniaOps
2
+ module Helper
3
+ module Docker
4
+ class << self
5
+ def image_tags(image)
6
+ code, data = get("/#{image}/tags/list")
7
+ return [] unless 200.eql? code
8
+ data['tags']
9
+ end
10
+
11
+ def remote_tag(image_name_and_tag, target_image_name_and_tag=nil, force=false)
12
+ target_image_name_and_tag = image_name_and_tag if target_image_name_and_tag.nil?
13
+ opts = force ? '-f' : ''
14
+ OdaniaOps::Helper::Shell.execute("docker tag #{opts} #{image_name_and_tag} #{registry_name}/#{target_image_name_and_tag}")
15
+ end
16
+
17
+ def push(image_name)
18
+ OdaniaOps::Helper::Shell.execute("docker push #{registry_name}/#{image_name}")
19
+ end
20
+
21
+ def login
22
+ $logger.info "Loggin in to private registry #{registry_name}"
23
+ data = $config['docker']
24
+ puts data.inspect
25
+ OdaniaOps::Helper::Shell.execute("docker login --username=#{data['user']} --password=\"#{data['password']}\" --email=#{data['email']} #{registry_url}")
26
+ end
27
+
28
+ private
29
+
30
+ def auth
31
+ {:username => $config['docker']['user'], :password => $config['docker']['password']}
32
+ end
33
+
34
+ def registry_name
35
+ uri = URI.parse $config['docker']['url']
36
+ uri.host
37
+ end
38
+
39
+ def registry_url
40
+ uri = URI.parse $config['docker']['url']
41
+ uri.path = '/v2'
42
+ uri.to_s
43
+ end
44
+
45
+ def get(query_url)
46
+ $logger.debug "Query registry: #{registry_url}#{query_url}"
47
+ response = HTTParty.get("#{registry_url}#{query_url}", :basic_auth => auth)
48
+ puts response.inspect
49
+ puts response.code
50
+ puts response.parsed_response.inspect
51
+ return response.code, response.parsed_response
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,14 @@
1
+ module OdaniaOps
2
+ module Helper
3
+ module Shell
4
+ class << self
5
+ def execute(cmd)
6
+ unless system(cmd)
7
+ $logger.error "Error executing command '#{cmd}'"
8
+ raise "Error executing command '#{cmd}'"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module OdaniaOps
2
+ VERSION = '0.0.1'
3
+ end
data/lib/odania_ops.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'odania_ops/version'
2
+ require 'thor'
3
+ require 'httparty'
4
+ require 'logger'
5
+ require 'yaml'
6
+ require 'deep_merge/rails_compat'
7
+
8
+ require_relative 'odania_ops/cli/config'
9
+ require_relative 'odania_ops/cli/docker'
10
+ require_relative 'odania_ops/helper/config'
11
+ require_relative 'odania_ops/helper/docker'
12
+ require_relative 'odania_ops/helper/shell'
13
+
14
+ # Setup logger
15
+ $logger = Logger.new(STDOUT)
16
+ $logger.level = Logger::INFO
17
+
18
+ # Load Config
19
+ OdaniaOps::Helper::Config.load_config(__FILE__)
20
+
21
+ module OdaniaOps
22
+ module Cli
23
+ class Application < Thor
24
+ desc 'docker', 'Docker helper'
25
+ subcommand 'docker', Docker
26
+
27
+ desc 'config', 'Manage configuration'
28
+ subcommand 'config', Config
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/odania_ops/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Mike Petersen']
6
+ gem.email = ['mike@odania-it.com']
7
+ gem.description = %q{Ops tools for managing servers}
8
+ gem.summary = %q{Ops tools for managing servers}
9
+ gem.homepage = 'http://www.odania.com/ops-tools'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = %w(ops odania-ops)
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = 'odania_ops'
15
+ gem.require_paths = ['lib']
16
+ gem.version = OdaniaOps::VERSION
17
+ gem.license = 'MIT'
18
+
19
+ gem.add_dependency 'thor'
20
+ gem.add_dependency 'docker-api'
21
+ gem.add_dependency 'httparty'
22
+ gem.add_dependency 'deep_merge'
23
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: odania_ops
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Petersen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-03 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: docker-api
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: deep_merge
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Ops tools for managing servers
70
+ email:
71
+ - mike@odania-it.com
72
+ executables:
73
+ - ops
74
+ - odania-ops
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - bin/odania-ops
85
+ - bin/ops
86
+ - lib/odania_ops.rb
87
+ - lib/odania_ops/cli/config.rb
88
+ - lib/odania_ops/cli/docker.rb
89
+ - lib/odania_ops/helper/config.rb
90
+ - lib/odania_ops/helper/docker.rb
91
+ - lib/odania_ops/helper/shell.rb
92
+ - lib/odania_ops/version.rb
93
+ - odania_ops.gemspec
94
+ homepage: http://www.odania.com/ops-tools
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Ops tools for managing servers
118
+ test_files: []