Class: Zillabyte::API::Flows

Inherits:
Base
  • Object
show all
Defined in:
lib/zillabyte/api/flows.rb

Direct Known Subclasses

Apps, Components

Instance Method Summary collapse

Methods inherited from Base

#initialize, #request

Methods included from Helpers

#app, #ask, #command, #create_git_remote, #display, #error, #extract_app_from_git_config, #extract_app_in_dir, #format_with_bang, #friendly_dir, #get_flow_ui_link, #get_info, #get_rich_info, #git, #handle_downloading_manifest, #has_git?, #longest, #read_multiline, #truncate_message, #with_tty

Constructor Details

This class inherits a constructor from Zillabyte::API::Base

Instance Method Details

#create_delete(id, options = {}) ⇒ Object

POST /flows/id/delete



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zillabyte/api/flows.rb', line 53

def create_delete(id, options = {})

  options = {
    # TODO
  }.merge(options)

  res = @api.request(
    :expects  => 200,
    :method   => :post,
    :path     => "/flows/#{id}/delete",
    :body     => options.to_json
  )

  res.body

end

#create_kill(id, options = {}) ⇒ Object

POST /flows/id/kill



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zillabyte/api/flows.rb', line 17

def create_kill(id, options = {})

  options = {
    # TODO
  }.merge(options)

  res = @api.request(
    :expects  => 200,
    :method   => :post,
    :path     => "/flows/#{id}/kill",
    :body     => options.to_json
  )

  res.body

end

#delete(id, options = {}) ⇒ Object

DELETE /flows/id



6
7
8
9
10
11
12
13
14
# File 'lib/zillabyte/api/flows.rb', line 6

def delete(id, options ={})
  res = @api.request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/flows/#{id}",
    :body     => options.to_json
  )
  res.body
end

#poll_delete(id, options = {}) ⇒ Object

GET /flows/id/delete



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/zillabyte/api/flows.rb', line 71

def poll_delete(id, options = {})

  options = {
    # TODO
  }.merge(options)

  res = @api.request(
    :expects  => 200,
    :method   => :get,
    :path     => "/flows/#{id}/delete",
    :body     => options.to_json
  )

  res.body

end

#poll_kill(id, options = {}) ⇒ Object

GET /flows/id/kill



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zillabyte/api/flows.rb', line 35

def poll_kill(id, options = {})

  options = {
    # TODO
  }.merge(options)

  res = @api.request(
    :expects  => 200,
    :method   => :get,
    :path     => "/flows/#{id}/kill",
    :body     => options.to_json
  )

  res.body

end

#pull_to_directory(id, dir, session = nil, options = {}) ⇒ Object

GET /flows/id/download



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/zillabyte/api/flows.rb', line 89

def pull_to_directory(id, dir, session = nil, options = {})
  require("zillabyte/common/tar")
  require("stringio")
  # Get the resource. note query params
  type = options[:output_type]
  session.display "downloading ##{id}" if session && type.nil?
  res = @api.request(
    :expects  => 200,
    :method   => :get,
    :path     => "/flows/#{id}/download",
    :body     => options.to_json
  )

  hash = res.body
  if hash['status'] == 'error'
    error hash['error_message']
  else
    if(hash['tar'])
      session.display "unpacking to #{friendly_dir(dir)}" if type.nil?
      require("base64")
      tar = StringIO.new(Base64.decode64(hash['tar']))
      hash.delete('tar')
      Zillabyte::Common::Tar.untar(tar, dir)
    #If you get an uri instead of the tar, then download the file directly from s3
    elsif(hash['uri'])
      require("net/http")
      session.display "downloading ##{id} to #{friendly_dir(dir)}" if type.nil?
      uri = URI(hash['uri'])
      try_again = 1
      while(try_again)
        Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
          request = Net::HTTP::Get.new(uri.request_uri)
          response = http.request(request)
          if response.code.to_i >= 300
            try_again = 1
            break
          end
          tar = StringIO.new(response.body)
          Zillabyte::Common::Tar.untar(tar, dir)
          try_again = nil
        end
      end
      hash.delete('uri')
    end
  end
  
  hash
  
end

#push_directory(dir, hash, session = nil, options = {}) ⇒ Object

POST /apps OR /components



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/zillabyte/api/flows.rb', line 141

def push_directory(dir, hash, session = nil, options = {})
  require("zillabyte/common/tar")
  type = options[:output_type]
  flow_type = hash["flow_type"]

  # Tar up everything... 
  hash['ignore_files'] ||= []
  hash['ignore_files'] << "info_to_ruby.in"
  hash['ignore_files'] << "vEnv"
  hash['ignore_files'] << "vendor"

  top_dir = hash['top_dir'] || "."
  ignore_files = hash['ignore_files'] || []
  session.display("packaging directory... ", false) if session && type.nil?
  tar = Zillabyte::Common::Tar.tar(top_dir, ignore_files)
  session.display("done") if session && type.nil?

  hash.delete('pwd')

  options = {
    :name => hash['name'],
    :meta => hash,
    # :tar => Base64.encode64(tar.read)
  }.merge(options)

  session.display("uploading #{tar.size} bytes... ", false) if session && type.nil?
  res = @api.request(
    :expects  => 200,
    :method   => :post,
    :path     => "/#{flow_type}s",
    :body     => options.to_json
  )


  if(res.body['uri'])
    require("net/http")

    uri = URI(res.body['uri'])
    try_again = 1
    while(try_again)
      Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
        request = Net::HTTP::Put.new(uri.request_uri)
        request.body_stream = tar
        request['Content-Length'] = request.body_stream.size
        request['Content-Type'] = ''
        response = http.request(request)
        if response.code.to_i >= 300
          try_again = 1
          break
        end
        try_again = nil
      end
    end
    res.body.delete('uri')
  end
  session.display("done")if session && type.nil?
  res.body

end