Module: Dawn::CLI::App

Extended by:
BaseCommands
Defined in:
lib/dawn/cli/commands/app.rb

Class Method Summary collapse

Methods included from BaseCommands

command, handle_abort_exception

Methods included from Helpers

#current_app, #current_app_name, #extract_app_in_dir, #extract_app_remote_from_git_config, #git, #git_add_dawn_remote, #git_dawn_remote?, #git_remotes, #git_remove_dawn_remote, #has_git?, #try_create_app

Methods included from OutputFormatter

#format_apps, #format_domains, #format_drains, #format_gears, #format_keys, #format_releases, #table_style

Class Method Details

.create(appname = nil) ⇒ Object

“Create a new dawn App (with git; setup)”



13
14
15
16
17
18
19
20
# File 'lib/dawn/cli/commands/app.rb', line 13

def self.create(appname=nil)
  app = try_create_app appname
  # since its possible for dawn to create a new app, with a random name
  # setting the appname again based on the real app's name is required
  appname = app.name
  git_add_dawn_remote app
  say "\tAPP\t#{app.name}"
end

.deleteObject

“Deletes the app on dawn”



58
59
60
61
62
63
# File 'lib/dawn/cli/commands/app.rb', line 58

def self.delete
  app = current_app

  app.destroy
  git_remove_dawn_remote app
end

.listObject

“Displays a list of all the apps you have deployed to dawn”



25
26
27
# File 'lib/dawn/cli/commands/app.rb', line 25

def self.list
  say format_apps(Dawn::App.all)
end

.list_gearsObject

“Lists all currently running Gears”



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dawn/cli/commands/app.rb', line 118

def self.list_gears
  app = current_app
  gears = app.gears.all.sort_by(&:number)

  ## Print all Gears
  gears_by_type = gears.each_with_object({}) do |gear, hsh|
    (hsh[gear.type] ||= []) << gear
  end
  gears_by_type.keys.sort.each do |key|
    grs = gears_by_type[key]
    say "=== #{key}:"
    say format_gears grs
  end
end

.logs(filters, follow = false) ⇒ Object

“Prints the App’s log to STDOUT”



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dawn/cli/commands/app.rb', line 70

def self.logs(filters, follow=false)
  # this is the only method which requires the uri & net/http
  require 'uri'
  require 'net/http'

  app = current_app

  filter_regex = %r{\A(?<timestamp>\S+)\s(?<token>\S+)\[(?<proc_id>\S+)\]\:(?<message>.*)}
  timestamp_regex = %r{(?<year>\d+)-(?<month>\d+)-(?<day>\d+)T(?<hour>\d+)\:(?<minute>\d+)\:(?<second>\d+)\.(?<other>.*)}

  opts = {}
  opts[:tail] = follow
  url = app.logs(opts)
  uri  = URI.parse(url)

  begin
    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = 60 * 60 * 24
    begin
      http.start do
        link_url = uri.path + ("?srv=1")
        #say uri.host + ":" + uri.port.to_s + link_url
        http.request_get(link_url) do |request|
          request.read_body do |chunk|
            if filters.size > 0
              chunk.each_line do |line|
                if mtch_data = line.chomp.match(filter_regex)
                  say mtch_data[0] if filters.include?(mtch_data[:proc_id])
                end
              end
            else
              say chunk.to_s
            end
          end
        end
      end
    rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError
      raise "Could not connect to logging service"
    rescue Timeout::Error, EOFError
      raise "\nRequest timed out"
    end
  rescue Interrupt
  end
end

.restartObject

“Restart the current app”



136
137
138
# File 'lib/dawn/cli/commands/app.rb', line 136

def self.restart
  current_app.restart
end

.run(argv) ⇒ Object

“run a command in the current_app”



144
145
146
147
148
# File 'lib/dawn/cli/commands/app.rb', line 144

def self.run(argv)
  current_app.run(command: argv.join(" "))
rescue Excon::Errors::BadRequest => ex
  handle_abort_exception("dawn run", ex)
end

.scale(modifiers) ⇒ Object

“Modify the gears of the current app”



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dawn/cli/commands/app.rb', line 34

def self.scale(modifiers)
  app = current_app

  #formation = app.formation.dup
  formation = {}

  modifiers.each do |type, a|
    operator, value = *a

    old_formation = (app.formation[type] || 0).to_i

    formation[type] = case operator
                      when "+" then old_formation + value.to_i
                      when "-" then old_formation - value.to_i
                      when "=" then value.to_i
                      end
  end

  app.scale(app: { formation: formation })
end