Class: 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:) ⇒ AwsBackend

Returns a new instance of AwsBackend.



7
8
9
10
11
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 7

def initialize(app_name:, region:)
  @app_name = app_name
  @aws_name = @app_name.gsub("_", "-")
  @region = region
end

Instance Attribute Details

#access_idObject

Returns the value of attribute access_id.



5
6
7
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 5

def access_id
  @access_id
end

#bucket_nameObject

Returns the value of attribute bucket_name.



5
6
7
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 5

def bucket_name
  @bucket_name
end

#regionObject

Returns the value of attribute region.



5
6
7
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 5

def region
  @region
end

#secret_keyObject

Returns the value of attribute secret_key.



5
6
7
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 5

def secret_key
  @secret_key
end

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 17

def create
  bucket_configuration = ''
  bucket_configuration += "--create-bucket-configuration LocationConstraint=#{@region}" unless @region.eql? 'us-east-1'
  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, @secret_key = aws_credentials_json["AccessKeyId"], aws_credentials_json["SecretAccessKey"]
  return @access_id, @secret_key
end

#create_topic_subscription(endpoint) ⇒ Object



48
49
50
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 48

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

#destroyObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 52

def destroy
  aws_command "aws s3api delete-bucket --bucket #{@aws_name} --region #{@region}"
  
  aws_command("aws sns list-topics")["Topics"].each do |topic|
    topic_arn = topic["TopicArn"]
    aws_command "aws sns delete-topic --topic-arn '#{topic_arn}'" if topic_arn.end_with?(@aws_name)
  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



32
33
34
35
36
37
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 32

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



39
40
41
42
43
44
45
46
# File 'lib/mobile_workflow_cli/aws_backend.rb', line 39

def write_env
  open('.env', 'a') { |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