-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from DeployGate/work/add_devices_server
Add add-devices --server option
- Loading branch information
Showing
9 changed files
with
169 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module DeployGate | ||
class AddDevicesServer | ||
|
||
def start(token, owner_name, bundle_id, distribution_key, args, options) | ||
DeployGate::Xcode::MemberCenter.instance | ||
options.server = false | ||
|
||
puts I18n.t('command_builder.add_devices.server.connecting') | ||
res = DeployGate::API::V1::Users::Apps::CliWebsockets.create(token, owner_name, bundle_id, distribution_key) | ||
|
||
server = res[:webpush_server] | ||
push_token = res[:push_token] | ||
action = res[:action] | ||
if res[:error] || server.blank? || push_token.blank? || action.blank? | ||
raise res[:message] | ||
end | ||
|
||
websocket_setup(server, bundle_id, push_token, action, args, options) do |socket| | ||
puts HighLine.color(I18n.t('command_builder.add_devices.server.start'), HighLine::GREEN) | ||
|
||
Workers::PeriodicTimer.new(60) do | ||
DeployGate::API::V1::Users::Apps::CliWebsockets.heartbeat(token, owner_name, bundle_id, distribution_key, push_token) | ||
end | ||
|
||
Signal.trap(:INT){ | ||
socket.disconnect | ||
exit 0 | ||
} | ||
end | ||
|
||
loop do | ||
sleep 60 | ||
end | ||
end | ||
|
||
def self.build(pool, bunlde_id, iphones, args, options) | ||
iphones.reject! { |iphone| iphone['is_registered'] } # remove udids if already registered | ||
devices = iphones.map do |iphone| | ||
udid = iphone['udid'] | ||
device_name= iphone['device_name'] | ||
DeployGate::Xcode::MemberCenters::Device.new(udid, '', device_name) | ||
end | ||
return if devices.empty? | ||
|
||
puts HighLine.color(I18n.t('command_builder.add_devices.server.start_build'), HighLine::GREEN) | ||
pool.perform do | ||
DeployGate::Commands::AddDevices.register!(devices) | ||
DeployGate::Commands::AddDevices.build!(bunlde_id, args, options) | ||
puts HighLine.color(I18n.t('command_builder.add_devices.server.finish_build'), HighLine::GREEN) | ||
puts '' | ||
end | ||
end | ||
|
||
private | ||
|
||
def websocket_setup(server, bundle_id, push_token, target_action, args, options, &block) | ||
socket = SocketIO::Client::Simple.connect server | ||
socket.on :connect do | ||
socket.emit :subscribe, push_token | ||
block.call(socket) | ||
end | ||
|
||
socket.on :error do | ||
raise 'Socket Error' | ||
end | ||
|
||
pool = Workers::Pool.new(size: 1, on_exception: proc { |e| | ||
raise e | ||
}) | ||
socket.on push_token do |push_data| | ||
return if push_data['action'] != target_action | ||
data = JSON.parse(push_data['data']) | ||
|
||
iphones = data['iphones'] | ||
DeployGate::AddDevicesServer.build(pool, bundle_id, iphones, args, options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module DeployGate::API::V1::Users::Apps | ||
class CliWebsockets | ||
ENDPOINT = "/users/%s/platforms/%s/apps/%s/cli_websockets" | ||
|
||
class << self | ||
def create(token, name, package_name, distribution_key, platform = 'ios') | ||
params = {distribution_access_key: distribution_key} unless distribution_key.nil? | ||
res = DeployGate::API::V1::Base.new(token).post(sprintf(ENDPOINT, name, platform, package_name), params || {}) | ||
|
||
results = { | ||
error: res['error'] | ||
} | ||
if results[:error] | ||
results.merge!( | ||
{ | ||
message: res['message'] | ||
} | ||
) | ||
else | ||
results.merge!( | ||
{ | ||
push_token: res['results']['push_token'], | ||
webpush_server: res['results']['webpush_server'], | ||
action: res['results']['action'] | ||
} | ||
) | ||
end | ||
|
||
results | ||
end | ||
|
||
def heartbeat(token, name, package_name, distribution_key, push_token, platform = 'ios') | ||
params = {distribution_access_key: distribution_key} unless distribution_key.nil? | ||
res = DeployGate::API::V1::Base.new(token).get("#{sprintf(ENDPOINT, name, platform, package_name)}/#{push_token}/heartbeat", params || {}) | ||
|
||
{ | ||
error: res['error'] | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module DeployGate | ||
VERSION = "0.2.3" | ||
VERSION = "0.3.0" | ||
end |