Class: Ubalo::Pod

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ubalo/pod.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

append_config, columns, debug_mode?, decode_content, get_targz, indent, login_suggestion, normalize_pod_name, pluralize, put_targz, read_config, time_ago_in_words, write_config

Constructor Details

#initialize(account, username, name, attributes) ⇒ Pod

Returns a new instance of Pod.



11
12
13
14
15
16
# File 'lib/ubalo/pod.rb', line 11

def initialize(, username, name, attributes)
  @account = 
  @username = username
  @name = name
  update_attributes(attributes)
end

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive.



9
10
11
# File 'lib/ubalo/pod.rb', line 9

def archive
  @archive
end

#code_updated_atObject (readonly)

Returns the value of attribute code_updated_at.



9
10
11
# File 'lib/ubalo/pod.rb', line 9

def code_updated_at
  @code_updated_at
end

#compilation_processObject (readonly)

Returns the value of attribute compilation_process.



9
10
11
# File 'lib/ubalo/pod.rb', line 9

def compilation_process
  @compilation_process
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/ubalo/pod.rb', line 9

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/ubalo/pod.rb', line 9

def state
  @state
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/ubalo/pod.rb', line 9

def username
  @username
end

Class Method Details

.create_with_json(account, json) ⇒ Object



5
6
7
# File 'lib/ubalo/pod.rb', line 5

def self.create_with_json(, json)
  new(, json.fetch('user').fetch('username'), json.fetch('name'), json)
end

Instance Method Details

#==(other) ⇒ Object



150
151
152
# File 'lib/ubalo/pod.rb', line 150

def ==(other)
  fullname == other.fullname
end

#clone_to(pod_dir) ⇒ Object



112
113
114
115
116
# File 'lib/ubalo/pod.rb', line 112

def clone_to(pod_dir)
  refresh!
  archive.extract_to(pod_dir)
  pod_dir.write_name(fullname)
end

#compiled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/ubalo/pod.rb', line 118

def compiled?
  state == 'compiled'
end

#delete!Object



122
123
124
125
# File 'lib/ubalo/pod.rb', line 122

def delete!
  request(:delete, nil, :parse => false)
  @deleted = true
end

#deleted?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/ubalo/pod.rb', line 127

def deleted?
  !!@deleted
end

#full_printable_resultObject



138
139
140
141
142
143
144
# File 'lib/ubalo/pod.rb', line 138

def full_printable_result
  s = printable_result
  if compilation_process
    s << compilation_process.printable_result
  end
  s
end

#fullnameObject



30
31
32
# File 'lib/ubalo/pod.rb', line 30

def fullname
  "#{@username}/#{@name}"
end

#inspectObject



146
147
148
# File 'lib/ubalo/pod.rb', line 146

def inspect
  "#<Pod #{fullname.inspect} state=#{state.inspect}>"
end

#latest_taskObject



64
65
66
67
68
69
70
# File 'lib/ubalo/pod.rb', line 64

def latest_task
  if task_json = request(:get, "/tasks/latest")
    @account.task_from_json(task_json)
  else
    raise Ubalo::Error, "Could not find latest task for #{fullname.inspect}"
  end
end

#printable_resultObject



131
132
133
134
135
136
# File 'lib/ubalo/pod.rb', line 131

def printable_result
  s = ""
  s << "  name: #{fullname}\n"
  s << " state: #{state}\n"
  s
end

#push_from(pod_dir, detached = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ubalo/pod.rb', line 72

def push_from(pod_dir, detached=false)
  tailer = Ubalo::TimedTailer.new(logger)
  tailer.update("Pushing files to #{fullname}...")

  pod_dir.assert_not_empty!

  update!
  new_archive.activate_from(pod_dir)


  logger.poll do
    tailer.update("Pushed files. Waiting to build #{fullname}...")
    refresh!
    return if detached
    compilation_process
  end

  compilation_process.tail(tailer, "Pushed files. Building #{fullname}...") do
    refresh!
    compiled?
  end

  tailer.update("Pushed #{fullname}. Exit code: #{compilation_process.exit_result}.\n")
end

#refresh!Object



24
25
26
27
28
# File 'lib/ubalo/pod.rb', line 24

def refresh!
  update_attributes(request(:get))
rescue RestClient::ResourceNotFound
  raise Ubalo::Error, "Could not find pod #{fullname.inspect}"
end

#run(version2, argv, arg) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ubalo/pod.rb', line 40

def run(version2, argv, arg)
  if version2
    if argv.empty?
      raise Ubalo::Error, "Specify the command to run"
    end
  else
    arg = argv.shift || arg
    argv = ["sh", "run.sh"]
  end

  params = {
    :arg_content_type => UbaloJSON.content_type,
    :arg => UbaloJSON.dump(arg),
    :argv => argv,
  }
  task = @account.task_from_json(request(:post, "/tasks", :params => params))

  task
rescue RestClient::Conflict
  raise Ubalo::Error, "Cannot run pod while compiling"
rescue RestClient::ResourceNotFound
  raise Ubalo::Error, "Pod not found"
end

#stop!Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ubalo/pod.rb', line 97

def stop!
  tailer = Ubalo::TimedTailer.new(logger)

  tailer.update("Stopping #{fullname}...")
  request(:put, "/stop")

  logger.poll do
    tailer.update("Stopping #{fullname}...")
    refresh!
    compiled?
  end

  tailer.update("Stopped #{fullname}. Compilation exit code: #{compilation_process.exit_result}.\n")
end

#tasksObject



34
35
36
37
38
# File 'lib/ubalo/pod.rb', line 34

def tasks
  request(:get, "/tasks").map do |task_json|
    @account.task_from_json(task_json)
  end
end

#update!Object



18
19
20
21
22
# File 'lib/ubalo/pod.rb', line 18

def update!
  update_attributes(request(:put))
rescue RestClient::ResourceNotFound
  raise Ubalo::Error, "Could not update pod #{fullname.inspect}"
end

#urlObject



154
155
156
# File 'lib/ubalo/pod.rb', line 154

def url
  @account.url_for(path_prefix)
end