Class: MobileWorkflow::Cli::AwsBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/mobile_workflow/cli/aws_backend.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name:, region: 'us-east-1') ⇒ AwsBackend

Returns a new instance of AwsBackend.



10
11
12
13
14
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 10

def initialize(app_name:, region: 'us-east-1')
  @app_name = app_name
  @aws_name = @app_name.gsub('_', '-')
  @region = region
end

Instance Attribute Details

#access_idObject

Returns the value of attribute access_id.



8
9
10
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 8

def access_id
  @access_id
end

#bucket_nameObject

Returns the value of attribute bucket_name.



8
9
10
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 8

def bucket_name
  @bucket_name
end

#regionObject

Returns the value of attribute region.



8
9
10
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 8

def region
  @region
end

#secret_keyObject

Returns the value of attribute secret_key.



8
9
10
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 8

def secret_key
  @secret_key
end

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 20

def create
  bucket_configuration = ''
  unless @region.eql? 'us-east-1'
    bucket_configuration += "--create-bucket-configuration LocationConstraint=#{@region}"
  end
  aws_command "aws s3api create-bucket --bucket #{@aws_name} --acl private --region #{@region} #{bucket_configuration}"
  @topic_arn = aws_command("aws sns create-topic --name #{@aws_name} --region #{@region}")['TopicArn']
  aws_command "aws iam create-user --user-name #{@aws_name}"
  aws_command "aws iam put-user-policy --user-name #{@aws_name} --policy-name s3 --policy-document '{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"s3:PutObject\",\"s3:PutObjectAcl\",\"s3:GetObject\", \"s3:DeleteObject\"],\"Resource\":[\"arn:aws:s3:::#{@aws_name}/*\"]}, {\"Effect\": \"Allow\", \"Action\": \"s3:ListBucket\", \"Resource\": \"arn:aws:s3:::#{@aws_name}\"}]}'"
  aws_command "aws iam put-user-policy --user-name #{@aws_name} --policy-name sns --policy-document '{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"sns:ConfirmSubscription\"],\"Resource\":[\"#{@topic_arn}\"]}]}'"
  aws_command "aws sns set-topic-attributes --topic-arn #{@topic_arn} --region #{@region} --attribute-name Policy --attribute-value '{\"Version\": \"2012-10-17\", \"Id\": \"s3\", \"Statement\": [{\"Sid\": \"#{@aws_name}-s3-sid\", \"Effect\": \"Allow\", \"Principal\": {\"AWS\": \"*\"}, \"Action\": \"SNS:Publish\", \"Resource\": \"#{@topic_arn}\", \"Condition\": {\"ArnEquals\": {\"aws:SourceArn\": \"arn:aws:s3:::#{@aws_name}\"}}}]}'"
  aws_command "aws s3api put-bucket-notification-configuration --bucket #{@aws_name} --notification-configuration '{\"TopicConfigurations\": [{\"TopicArn\": \"#{@topic_arn}\", \"Events\": [\"s3:ObjectCreated:*\"]}]}'"
  aws_credentials_json = aws_command("aws iam create-access-key --user-name #{@aws_name}")['AccessKey']
  @access_id = aws_credentials_json['AccessKeyId']
  @secret_key = aws_credentials_json['SecretAccessKey']
  [@access_id, @secret_key]
end

#create_topic_subscription(endpoint) ⇒ Object



54
55
56
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 54

def create_topic_subscription(endpoint)
  aws_command "aws sns subscribe --topic-arn #{@topic_arn} --region #{@region} --protocol https --notification-endpoint #{endpoint}"
end

#destroyObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 58

def destroy
  aws_command "aws s3api delete-bucket --bucket #{@aws_name} --region #{@region}"

  aws_command("aws sns list-topics --region #{@region}")['Topics'].each do |topic|
    topic_arn = topic['TopicArn']
    if topic_arn.end_with?(@aws_name)
      aws_command "aws sns delete-topic --topic-arn '#{topic_arn}' --region #{@region}"
    end
  end

  aws_command "aws iam delete-user-policy --user-name #{@aws_name} --policy-name s3"
  aws_command "aws iam delete-user-policy --user-name #{@aws_name} --policy-name sns"
  aws_command("aws iam list-access-keys --user-name #{@aws_name}")['AccessKeyMetadata'].each do ||
    aws_command "aws iam delete-access-key --user-name #{@aws_name} --access-key #{['AccessKeyId']}"
  end
  aws_command "aws iam delete-user --user-name #{@aws_name}"
end

#put_envObject



38
39
40
41
42
43
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 38

def put_env
  puts "AWS_ACCESS_ID=#{access_id}"
  puts "AWS_SECRET_KEY=#{secret_key}"
  puts "AWS_REGION=#{region}"
  puts "AWS_BUCKET_NAME=#{bucket_name}"
end

#write_envObject



45
46
47
48
49
50
51
52
# File 'lib/mobile_workflow/cli/aws_backend.rb', line 45

def write_env
  open('.env', 'a') do |f|
    f.puts "AWS_ACCESS_ID=#{access_id}"
    f.puts "AWS_SECRET_KEY=#{secret_key}"
    f.puts "AWS_REGION=#{region}"
    f.puts "AWS_BUCKET_NAME=#{bucket_name}"
  end
end