require 'meroku/cli/help' require 'meroku/cli/secrets' require 'meroku/cli/session' module Meroku # The CLI is used by end users and also maintainers module CLI include Meroku::Aws::Ec2 def signup email, pw = Meroku::Extensions.mgets(%i[email password]) Meroku::Api::Request.post( 'https://www.meroku.com/users.json', user: { email: email, password: pw, password_confirmation: pw } ) do |response| puts "Signed up #{response['attributes']['email']}" save_setting('apiusername', response['attributes']['apiusername']) save_setting('apisecret', response['attributes']['apisecret']) end true end def login email, password = Meroku::Extensions.mgets(%i[email password]) Meroku::Api::Request.post( 'https://www.meroku.com/users/sign_in.json', user: { email: email, password: password } ) do |response| puts "Signed in as #{response['attributes']['email']}" save_setting('apiusername', response['attributes']['apiusername']) save_setting('apisecret', response['attributes']['apisecret']) end true end def logout [ "#{Dir.home}/.meroku/.apiusername", "#{Dir.home}/.meroku/.apisecret" ].each do |f| File.delete(f) if File.exist?(f) end true end def create Meroku::Api::Request.post( 'https://www.meroku.com/apps.json', app: { name: 'unnamed' }, apisecret: `cat ~/.meroku/.apisecret`.chomp ) do |response| puts "Created #{response['attributes']['name']}, adding git remote" add_git_remote( 'meroku', "#{apiusername}@www.meroku.com:#{response['attributes']['name']}.git" ) end true end def apiusername `cat ~/.meroku/.apiusername.chomp` end def add_git_remote(name, uri) puts "git remote remove #{name}" `git remote remove #{name} 2>/dev/null` puts "git remote add #{name} #{uri}" `git remote add #{name} #{uri}` end def keys_add file = "#{Dir.home}/.ssh/id_rsa.pub" apisecret = `cat ~/.meroku/.apisecret`.chomp abort "error: #{file} not found" unless File.exist?(file) Meroku::Api::Request.post( 'https://www.meroku.com/publickeys.json', publickey: { name: 'id_rsa.pub', data: `cat ~/.ssh/id_rsa.pub`.chomp }, apisecret: apisecret ) do |response| puts "Added #{response['attributes']['name']}" end true end def spawn Meroku::CLI::Secrets.load(admin: true) node = Meroku::Node.new node.associate_address.configure_keys.add_sources.ubuntu_updates node.install_packages.database_inits.git_clone.nginx_configs.start_rails true end def despawn Meroku::CLI::Secrets.load(admin: true) Meroku::Infrastructure.each_server(&:shutdown) true end def save_setting(name, value) FileUtils.mkdir_p "#{Dir.home}/.meroku" IO.write("#{Dir.home}/.meroku/.#{name}", value) end end end