fidoci 0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c7383d0a36cc9491caea846a4a1554a692e866c
4
+ data.tar.gz: 57517f8b162f38ed5a7947219637ae85a2705d33
5
+ SHA512:
6
+ metadata.gz: b69803fc8a02b3e5a5934686c54ab289268994e82f6cf3d2b1d134d020f9c1ea26786238a74687f5132fe6a8321e9c19c67daf18c854b770e0310bb88fa960a5
7
+ data.tar.gz: 3f7e1720a5f2d0f6c4578cfb9c0fd7a70f1894f8d33628675824f9d79f74af6002c3d811f4c76667b9fc1baa8d6d5ae935834eac2f9a5489366422c70b4b7147
data/bin/d ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fidoci'
4
+
5
+ if ARGV.size == 0
6
+ puts "Usage: d OPTIONS"
7
+ puts "or d COMMAND [arg...]"
8
+ puts
9
+ puts "Dockerized development and build workflow."
10
+ puts
11
+ puts "Options:"
12
+ puts
13
+ puts " --build [--clean] tag Builds 'test' image, run tests inside and if successful, tag image by given 'tag'"
14
+ puts " --clean Removes all containers and images from docker related to this repo"
15
+ puts
16
+ puts "Command:"
17
+ puts " Running with command will build 'exec' image, start docker-compose with that image and run given command in container"
18
+ puts
19
+ puts "Example (for Rails):"
20
+ puts " d bundle exec rake db:migrate"
21
+ puts " d bundle exec rails s"
22
+ puts " d bundle exec rake"
23
+ exit 1
24
+ end
25
+
26
+ environment = Fidoci::Main.new
27
+
28
+ case ARGV[0]
29
+ when '--clean'
30
+ # d --clean
31
+ # clean service and intermediate docker images
32
+ environment.clean
33
+ when '--build'
34
+ # d --build [--clean] latest-staging
35
+ # build image, test it and if successful, tag as latest-staging
36
+ # if --clean is present, will clean all intermediate docker images after
37
+ if environment.build(ARGV.last, ARGV.size > 1 && ARGV[1] == '--clean')
38
+ exit 0
39
+ else
40
+ exit 1
41
+ end
42
+ else
43
+ # d cmd args args
44
+ # run cmd in exec environment
45
+ # build container image and run all services if not yet
46
+ environment.cmd(*ARGV)
47
+ end
data/lib/fidoci/env.rb ADDED
@@ -0,0 +1,66 @@
1
+ module Fidoci
2
+ # Environment configuration of D
3
+ # encapsulates container image building and running commands in it
4
+ class Env
5
+ attr_accessor :env_config
6
+
7
+ def initialize(image, name, env_config)
8
+ @name = name.to_s
9
+ @image = image
10
+ @env_config = env_config
11
+ end
12
+
13
+ def build_image()
14
+ if image_exists?
15
+ puts "Using exisitng image #{image_name}..."
16
+ return true
17
+ end
18
+
19
+ puts "Building image #{image_name}..."
20
+ params = []
21
+
22
+ params << '--force-rm=true'
23
+ params << "-t #{image_name}"
24
+ if env_config['dockerfile']
25
+ params << "-f #{env_config['dockerfile']}"
26
+ end
27
+
28
+ system "docker build #{params.join(' ')} ."
29
+ end
30
+
31
+ def image_name
32
+ "%s:%s" % [@image, @name]
33
+ end
34
+
35
+ def image_exists?
36
+ images = `docker images`
37
+ images_names = images.split("\n").drop(1).map{|line|
38
+ parts = line.split(/\s+/)
39
+ parts[0..1].join(':')
40
+ }
41
+
42
+ images_names.any?{|i| i == image_name}
43
+ end
44
+
45
+ def cmd(*args)
46
+ if build_image
47
+ puts "Running `#{args.join(' ')}`..."
48
+ system "docker-compose run --rm #{@name} #{args.join(' ')}"
49
+ else
50
+ puts "Build failed"
51
+ return false
52
+ end
53
+ end
54
+
55
+ def commands
56
+ return false unless env_config['commands']
57
+
58
+ success = env_config['commands'].all? { |command|
59
+ cmd command.split(/\s+/)
60
+ }
61
+
62
+ puts "Test failed" unless success
63
+ success
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,64 @@
1
+ require 'yaml'
2
+
3
+ module Fidoci
4
+ # Main entry point for D command
5
+ # reads configuration and provide high-level commands
6
+ class Main
7
+
8
+ attr_accessor :config
9
+
10
+ # Initialize entry point
11
+ # config_file - yml file path with configuration
12
+ def initialize(config_file = 'd.yml')
13
+ @config = YAML.load_file(config_file)
14
+ puts @config['image']
15
+ end
16
+
17
+ # Run command in default "exec" environment
18
+ # ie in container build and started with exec configuration
19
+ # args - command and arguments to pass to docker run command
20
+ def cmd(*args)
21
+ env(:exec).cmd(*args)
22
+ end
23
+
24
+ # Configured docker repository name
25
+ # image key from YAML file
26
+ def repository_name
27
+ config['image']
28
+ end
29
+
30
+ # Create environment instance with given name
31
+ # name - key that will be used to configure this env
32
+ def env(name)
33
+ Env.new(repository_name, name.to_s, config[name.to_s])
34
+ end
35
+
36
+ # Clean system
37
+ # removes all service and running containers and their images
38
+ # and removes all images build by d
39
+ def clean
40
+ system 'docker-compose kill'
41
+ system 'docker-compose rm -f'
42
+
43
+ (config.keys - ['image']).each { |name|
44
+ env = env(name)
45
+ system "docker rmi -f #{env.image_name}"
46
+ }
47
+ end
48
+
49
+ # Build image and run test in it
50
+ # tag - tag name to tag image after successful build and test
51
+ # do_clean - if true, will do clean after build (whether successful or not)
52
+ def build(tag, do_clean = false)
53
+ test_env = env(:test)
54
+ success = test_env.commands
55
+
56
+ if success
57
+ system "docker tag #{test_env.image_name} #{repository_name}:#{tag}"
58
+ end
59
+
60
+ clean if do_clean
61
+ success
62
+ end
63
+ end
64
+ end
data/lib/fidoci.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'fidoci/env'
2
+ require 'fidoci/main'
3
+
4
+ module Fidoci
5
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fidoci
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Dolezal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple tool around docker-compose to enable dockerized dev-2-test-2-production
14
+ workflow
15
+ email: lukas@dolezalu.cz
16
+ executables:
17
+ - d
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/d
22
+ - lib/fidoci.rb
23
+ - lib/fidoci/env.rb
24
+ - lib/fidoci/main.rb
25
+ homepage: https://github.com/DocX/fidoci
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.4.5
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Finally Docker CI
49
+ test_files: []
50
+ has_rdoc: