Module: Kibo::Commands

Extended by:
Forwardable, Commands
Included in:
Commands
Defined in:
lib/kibo/commands/spin.rb,
lib/kibo/commands.rb,
lib/kibo/commands/info.rb,
lib/kibo/commands/logs.rb,
lib/kibo/commands/setup.rb,
lib/kibo/commands/deploy.rb,
lib/kibo/commands/runner.rb,
lib/kibo/commands/helpers.rb,
lib/kibo/commands/compress.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commandsObject



17
18
19
# File 'lib/kibo/commands.rb', line 17

def self.commands
  public_instance_methods.map(&:to_s)
end

.descriptionsObject



8
9
10
# File 'lib/kibo/commands.rb', line 8

def self.descriptions
  @descriptions ||= {}
end

.optionsObject



4
5
6
# File 'lib/kibo/commands.rb', line 4

def self.options
  @options ||= {}
end

.subcommand(name, description = nil, &block) ⇒ Object



12
13
14
15
# File 'lib/kibo/commands.rb', line 12

def self.subcommand(name, description = nil, &block)
  options[name.to_s] = Proc.new if block_given?
  descriptions[name.to_s] = description
end

Instance Method Details

#compressObject



8
9
10
11
12
# File 'lib/kibo/commands/compress.rb', line 8

def compress
  Kibo::CommandLine.args.each do |path|
    compress_dir path
  end
end

#configure_instance(instance) ⇒ Object



85
86
87
# File 'lib/kibo/commands/setup.rb', line 85

def configure_instance(instance)
  heroku! "config:set", "INSTANCE=#{instance.instance_name}", "--app", instance
end

#configured_instancesObject



35
36
37
38
39
# File 'lib/kibo/commands/info.rb', line 35

def configured_instances
  sys("git remote", :quiet).split("\n").select { |line|
    line =~ /^#{Kibo.namespace}-#{Kibo.environment}/
  }
end

#create_instance(instance) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kibo/commands/setup.rb', line 40

def create_instance(instance)
  return false if sys("git remote | grep #{instance}", :quiet)

  heroku_url = "[email protected]:#{instance}.git"

  if Kibo::Heroku.apps.include?(instance)
    git :remote, :add, instance, heroku_url
  else
    heroku! "apps:create", instance, "--remote", instance
  end
  
  true
end

#deployObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kibo/commands/deploy.rb', line 4

def deploy
  ENV["ENVIRONMENT"] = Kibo.environment

  missing_instances = Kibo.config.instances - configured_instances
  unless missing_instances.empty?
    E "Use 'kibo setup #{Kibo.environment}' to set up these missing instances", *missing_instances
  end
  
  #
  # Run source commands
  with_commands :source do
    with_stashed_changes do
      # create a deployment branch, if there is none yet.
      in_branch "kibo.#{Kibo.environment}" do
        git "merge", "master"

        with_commands :arena do
          Kibo.config.instances.each do |instance| 
            git "push", "--force", instance, "HEAD:master"
          end
        end
      end
    end
  end
rescue FatalError
  raise
rescue StandardError
  W $!
  raise
end

#infoObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kibo/commands/info.rb', line 6

def info
  Kibo::Helpers::Info.print do |info|
    info.head "general"
    info.line "environment", Kibo.environment
    info.line "namespace", Kibo.namespace
    
    info.head "heroku"
    info.line "current account", Kibo::Heroku.whoami
    info.line "expected account", Kibo.config.heroku.

    info.head "processes"      
    info.line "mode", Kibo.config.heroku.mode
    Kibo.config.processes.each do |key, value|
      info.line key, value
    end

    # info.head "remotes"
    # 
    # info.line "expected", Kibo.expected_remotes
    # info.line "configured", Kibo.configured_remotes
    # info.line "missing", Kibo.missing_remotes

    info.head "instances"
    
    info.line "expected", Kibo.config.instances
    info.line "configured", configured_instances
  end
end

#list_heroku(what, instance) ⇒ Object



54
55
56
57
58
59
# File 'lib/kibo/commands/setup.rb', line 54

def list_heroku(what, instance)
  heroku!(what, "--app", instance, :quiet).
    split("\n").
    reject  { |line| line =~ /^=== / }.
    map     { |line| line.split(/\s+/).first }
end

#logObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kibo/commands/logs.rb', line 16

def log
  require "heroku"

  instance = Kibo::CommandLine.args.first 

  $stdout.sync = true
  
  client = Heroku::Auth.client
  client.read_logs(instance, [ "tail=1" ]) do |chunk|
    chunk.split("\n").each do |line|
      $stdout.puts line.split(": ", 2)[1]
    end
  end
end

#logsObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/kibo/commands/logs.rb', line 3

def logs
  require "foreman/engine/cli"

  cli = Foreman::Engine::CLI.new
  
  Kibo.config.instances.map do |remote|
    cli.register remote.split("-", 3).last, "#{Kibo.binary} log #{remote}"
  end
  
  cli.start
end

#per_instanceObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/kibo/commands/runner.rb', line 6

def per_instance
  if configured_instances.empty?
    E "No configured_instances in '#{Kibo.environment}' environment."
  end

  configured_instances.each do |instance|
    cmd = [ "heroku", *ARGV, "--app", instance ]
    W cmd.join(" ")
    system *cmd
  end
end

#per_roleObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kibo/commands/runner.rb', line 18

def per_role
  if configured_instances.empty?
    E "No configured_instances in '#{Kibo.environment}' environment."
  end

  configured_roles = configured_instances.group_by do |name|
    name.split("-").last.sub(/\d+$/, "")
  end
 
  instances = configured_roles.values.first
  
  instances.each do |instance|
    cmd = [ "heroku", *ARGV, "--app", instance ]
    W cmd.join(" ")
    system *cmd
  end
end

#provide_instance(instance) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kibo/commands/setup.rb', line 70

def provide_instance(instance)
  partial_instance_name = instance.split("-").last # e.g. "web1"

  instance_addons = Kibo.config.addons[partial_instance_name] || []
  return if instance_addons.empty? 

  existing_instance_addons = list_heroku("addons", instance)
  W "[#{instance}] addons", *existing_instance_addons

  missing = instance_addons - existing_instance_addons
  missing.each do |addon|
    heroku! "addons:add", addon, "--app", instance
  end
end

#reconfigureObject



30
31
32
33
34
35
36
37
38
# File 'lib/kibo/commands/setup.rb', line 30

def reconfigure
  # create all apps on heroku or make sure that they
  # exist as remotes in the local git configuration.
  Kibo.config.instances.each do |instance|
    provide_instance instance
    share_instance instance
    configure_instance instance
  end
end

#setupObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kibo/commands/setup.rb', line 13

def setup
  
  
  # create all apps on heroku or make sure that they
  # exist as remotes in the local git configuration.
  Kibo.config.instances.each do |instance|
    next unless create_instance(instance) || Kibo::CommandLine.force?

    # The following only when forced (--force) to do so 
    # or when a new instance has been created.

    provide_instance instance
    share_instance instance
    configure_instance instance
  end
end

#share_instance(instance) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/kibo/commands/setup.rb', line 61

def share_instance(instance)
  existing_sharings = list_heroku("sharing", instance)

  missing_sharings = Kibo.config.sharing - existing_sharings
  Kibo.config.sharing.each do |email|
    heroku! "sharing:add", email, "--app", instance
  end
end

#spindownObject



10
11
12
# File 'lib/kibo/commands/spin.rb', line 10

def spindown
  spin :down
end

#spinupObject



6
7
8
# File 'lib/kibo/commands/spin.rb', line 6

def spinup
  spin :up
end

#verify_heroku_loginObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/kibo/commands/setup.rb', line 89

def 
  whoami = Kibo::Heroku.whoami
  return if whoami == Kibo.config.heroku.

  if !whoami
    E "Please log in ('heroku auth:login') as #{Kibo.config.heroku.}."
  elsif whoami != Kibo.config.heroku.
    E "You are currently logged in as #{whoami}; please log in ('heroku auth:login') as #{Kibo.config.heroku.}."
  end
end