rat_deployer 0.1.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b70810b89cd248d2743394bee60594eb9f805b43
4
- data.tar.gz: 01c8aaf1c41c572a3247e908cae364b04ab774a9
3
+ metadata.gz: 06b1d076beea38ee47efb7915fdd163effbe94cb
4
+ data.tar.gz: d51100bfd44437364f06797353b0913f90ed508d
5
5
  SHA512:
6
- metadata.gz: 3e740ed48bc6b98d4943ea86ac77bc385a71594879819078062f5f9277db074b8b1fb286568b69033e5b23349567d87d4833343934fd1705f9f6fb52831d4b2c
7
- data.tar.gz: 62fe85d2ba81954e2b6ce5ddac32c87d67db0cc54d453002228021e44a56e575858526d622c92596cca194c54a43f4d2fe0c118d03fca2aaa41c395b1522f4ef
6
+ metadata.gz: 148b9cc68a5e73b0d5a1d552243bc0be8e69a99a6216451534120b168a69c0f452f09f559e7fc3cd79a8262f205b0790c1f90646be2ba65dacc6d9c7b8b9abdd
7
+ data.tar.gz: 47b5d856552c5a895176d2fc170ec68d5f481f454465ade1ff25d453199376b03dddf53ccf14903a5bc55e4ffad7f87daefda30a391719aa7abac0f19b41cf42
@@ -1,4 +1,5 @@
1
1
  require 'thor'
2
+ require 'yaml'
2
3
 
3
4
  require 'rat_deployer/config'
4
5
  require 'rat_deployer/command'
@@ -8,81 +9,104 @@ module RatDeployer
8
9
  class Images < Thor
9
10
  include RatDeployer::Command
10
11
 
11
- desc 'update [SERVICES...]', 'Update images for given services'
12
- def update(*services)
13
- services.each do |svc|
14
- put_heading "Updating image for service #{svc}"
15
- do_build(svc)
16
- do_push(svc)
12
+ desc 'update [SERVICES...]', 'Update images'
13
+ def update(*images)
14
+ get_images(images).each do |image|
15
+ put_heading "Updating image #{image}"
16
+
17
+ do_build(image)
18
+ do_push(image)
17
19
  end
18
20
  end
19
21
 
20
- desc 'build [SERVICES...]', 'Build images for given services'
21
- def build(*services)
22
- services.each &method(:do_build)
22
+ desc 'build [SERVICES...]', 'Build images'
23
+ def build(*images)
24
+ get_images(images).each &method(:do_build)
23
25
  end
24
26
 
25
- desc 'push [SERVICES...]', 'Push images for given services'
26
- def push(*services)
27
- services.each &method(:do_push)
27
+ desc 'push [SERVICES...]', 'Push images'
28
+ def push(*images)
29
+ get_images(images).each &method(:do_push)
28
30
  end
29
31
 
30
32
  private
31
33
 
32
- def do_build(service)
33
- put_heading "Building image for service #{service}"
34
-
35
- ensure_image_source(service)
34
+ def get_images(images)
35
+ images.any? ? images : all_images
36
+ end
36
37
 
37
- run "docker build #{source_path(service)} -t #{image_name(service)}"
38
+ def all_images
39
+ docker_conf = YAML.load(`RAT_PROMPT=false rat compose config`)
40
+ images = docker_conf.fetch("services").map { |_, c| c.fetch("image") }.uniq
38
41
  end
39
42
 
40
- def do_push(service)
41
- put_heading "Pusinhg image for service #{service}"
43
+ def do_build(image)
44
+ put_heading "Building image #{image}"
45
+ return unless image_conf_is_present?(image)
46
+ ensure_image_source(image)
47
+ run "docker build #{source_path(image)} -t #{image}"
48
+ end
42
49
 
43
- run "docker push #{image_name(service)}"
50
+ def do_push(image)
51
+ put_heading "Pusinhg image #{image}"
52
+ return unless image_conf_is_present?(image)
53
+ run "docker push #{image}"
44
54
  end
45
55
 
46
- def ensure_image_source(service)
47
- if git_conf(service)
48
- git_clone_repo(service) unless File.exists? source_path(service)
49
- git_checkout(service)
50
- git_pull(service)
56
+ def ensure_image_source(image)
57
+ if git_conf(image)
58
+ git_clone_repo(image) unless source_present?(image)
59
+ git_fetch(image)
60
+ git_checkout(image)
51
61
  else
52
- unless File.exists? source_path(service)
53
- put_error <<-ERROR
54
- No source found for service #{service}."
55
- Either specify git[url] or provision the source for #{service} yourself.
56
- ERROR
62
+ unless source_present?(image)
63
+ put_error "Source for image #{image} is not present and no git config was provided for it. Either provide git url and optionally branch or provision the source yourself at #{source_path(image)}"
57
64
  end
58
65
  end
59
66
  end
60
67
 
61
- def git_clone_repo(service)
62
- url = git_conf(service).fetch("url")
63
- run "git clone #{url} sources/#{service}"
68
+ def git_clone_repo(image)
69
+ url = image_conf(image).fetch('git').fetch('url')
70
+ run "git clone #{url} #{source_path(image)}"
71
+ end
72
+
73
+ def git_checkout(image)
74
+ run "git -C #{source_path(image)} checkout -f #{git_branch(image)}"
75
+ run "git -C #{source_path(image)} reset --hard origin/#{git_branch(image)}"
64
76
  end
65
77
 
66
- def git_checkout(service)
67
- branch = git_conf(service).fetch("branch", "master")
68
- run "git -C #{source_path(service)} checkout -f #{branch}"
78
+ def git_fetch(image)
79
+ run "git -C #{source_path(image)} fetch origin #{git_branch(image)}"
69
80
  end
70
81
 
71
- def git_pull(service)
72
- branch = git_conf(service).fetch("branch", "master")
73
- run "git -C #{source_path(service)} pull origin #{branch}"
82
+ def git_branch(image)
83
+ git_conf(image).fetch('branch', 'master')
74
84
  end
75
85
 
76
- def source_path(service)
77
- File.expand_path("./sources/#{service}")
86
+ def git_conf(image)
87
+ image_conf(image)['git']
78
88
  end
79
89
 
80
- def git_conf(service)
81
- RatDeployer::Config.for_service(service)["git"]
90
+ def image_conf(image)
91
+ RatDeployer::Config.images[image]
82
92
  end
83
93
 
84
- def image_name(service)
85
- RatDeployer::Config.for_service(service).fetch("name")
94
+ def source_path(image)
95
+ folder = image_conf(image).fetch('source', image)
96
+ File.expand_path("./sources/#{folder}")
97
+ end
98
+
99
+ def source_present?(image)
100
+ File.exists? source_path(image)
101
+ end
102
+
103
+ def image_conf_is_present?(image)
104
+ if image_conf(image)
105
+ true
106
+ else
107
+ put_warning "No image config found for image #{image}. Skipping"
108
+ false
109
+ end
86
110
  end
87
111
  end
88
112
  end
@@ -12,18 +12,13 @@ module RatDeployer
12
12
 
13
13
  desc "deploy", "deploys current environment"
14
14
  def deploy
15
- images = RatDeployer::Config.for_env["images"]
15
+ unset_machine
16
+ run "rat images update"
16
17
 
17
- if images.any?
18
- unset_machine
19
- run "rat images update #{images.keys.join(' ')}"
20
- set_machine
18
+ within_machine do
21
19
  run "rat compose pull"
20
+ run "rat compose up -d"
22
21
  end
23
-
24
- set_machine
25
- run "rat compose up -d"
26
- unset_machine
27
22
  end
28
23
 
29
24
  desc "compose ARGS...", "runs docker-compose command with default flags"
@@ -42,23 +37,26 @@ module RatDeployer
42
37
  run "docker-compose #{flags} #{cmd} #{cmd_flags.join(" ")}"
43
38
  end
44
39
 
45
- desc "set_machine", "sets machine as configured in your rat_file"
40
+ private
41
+
46
42
  def set_machine
47
43
  machine = RatDeployer::Config.for_env.fetch("machine")
48
44
  run "eval $(docker-machine env #{machine})"
49
45
  end
50
46
 
51
- desc "unset_machine", "unsets machine"
52
47
  def unset_machine
53
48
  run "eval $(docker-machine env --unset)"
54
49
  end
55
50
 
56
- private
51
+ def within_machine(&block)
52
+ run "rat set_machine"
53
+ yield
54
+ ensure
55
+ run "rat unset_machine"
56
+ end
57
57
 
58
58
  def warn_if_running_on_remote_host
59
- machine = ENV["DOCKER_MACHINE"]
60
-
61
- if machine
59
+ if machine = ENV["DOCKER_MACHINE"]
62
60
  put_warning "Your docker client is pointing to the remote machine #{machine}"
63
61
  ask_for_confirmation
64
62
  end
@@ -4,24 +4,31 @@ require 'highline/import'
4
4
  module RatDeployer
5
5
  module Command
6
6
  def run(cmd)
7
- msg = "||=> Running command ".colorize(:blue) + "`#{cmd.colorize(:white)}`"
8
- puts msg
7
+ if prompt_enabled?
8
+ msg = "||=> Running command ".colorize(:blue) + "`#{cmd.colorize(:white)}`"
9
+ puts msg
10
+ end
11
+
9
12
  system cmd
10
13
  end
11
14
 
12
15
  def put_heading(str)
16
+ return unless prompt_enabled?
13
17
  puts "|| #{str}".colorize(:blue)
14
18
  end
15
19
 
16
20
  def put_error(str)
21
+ return unless prompt_enabled?
17
22
  puts "// #{str}".colorize(:red)
18
23
  end
19
24
 
20
25
  def put_warning(str)
26
+ return unless prompt_enabled?
21
27
  puts colorize_warning(str)
22
28
  end
23
29
 
24
30
  def ask_for_confirmation
31
+ return unless prompt_enabled?
25
32
  prompt = "Are you sure you want to continue?"
26
33
 
27
34
  a = ''
@@ -39,5 +46,11 @@ module RatDeployer
39
46
  def colorize_warning(str)
40
47
  "\\\\ #{str}".colorize(:yellow)
41
48
  end
49
+
50
+ private
51
+
52
+ def prompt_enabled?
53
+ ENV['RAT_PROMPT'] != "false"
54
+ end
42
55
  end
43
56
  end
@@ -11,16 +11,18 @@ module RatDeployer
11
11
  ENV.fetch('RAT_ENV')
12
12
  end
13
13
 
14
- def self.for_env(e = env)
15
- confs = all.fetch('environments')
16
- default_conf = confs['default'] || confs['base'] || {}
17
- env_conf = confs.fetch(e)
14
+ def self.images
15
+ all.fetch('images', {})
16
+ end
18
17
 
19
- default_conf.deep_merge env_conf
18
+ def self.environmental
19
+ all.fetch('environments', {})
20
20
  end
21
21
 
22
- def self.for_service(service)
23
- for_env.fetch('images').fetch(service)
22
+ def self.for_env(e = env)
23
+ default_conf = environmental.fetch('default', {})
24
+ env_conf = environmental.fetch(e)
25
+ default_conf.deep_merge(env_conf)
24
26
  end
25
27
  end
26
28
  end
@@ -1,3 +1,3 @@
1
1
  module RatDeployer
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rat_deployer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Oga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-02 00:00:00.000000000 Z
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor