-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.yml
102 lines (88 loc) · 2.89 KB
/
launch.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
- name: Launch twitterbot instance
hosts: localhost
vars_files:
- secrets.yml
tasks:
- name: Set up security group for twitter bot
ec2_group:
name: sg_grauniad_bot
description: Security group for Grauniad twitter bot
vpc_id: "{{ secrets.vpc_id }}"
region: "{{ secrets.cloud_region }}"
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: "{{ secrets.home_ip }}/32"
- name: Gather security group information
command: >
aws ec2 describe-security-groups
--filters Name=vpc-id,Values="{{secrets.vpc_id}}"
--filters Name=group-name,Values=sg_grauniad_bot
--region "{{secrets.cloud_region}}"
register: sec_groups_query
changed_when: false
- name: Process security groups list
set_fact:
sec_groups: "{{ sec_groups_query.stdout|from_json }}"
when: "sec_groups_query.stdout is defined and {{(sec_groups_query.stdout|length)>0}}"
- name: Launch twitter bot machine
ec2:
region: "{{ secrets.cloud_region }}"
instance_type: t2.nano
image: ami-692d751a
count_tag:
Name: "Grauniad twitter bot"
exact_count: "{{server_count}}"
instance_tags:
Name: "Grauniad twitter bot"
Launcher: Ansible
group_id: "{{ sec_groups.SecurityGroups | map(attribute='GroupId') | list }}"
assign_public_ip: True
vpc_subnet_id: "{{ secrets.vpc_subnet_id }}"
key_name: "{{ secrets.key_name }}"
wait: True
register: ec2_twitterbots
- name: Add new instance to host group
add_host: hostname={{ item.public_ip }} groupname=twitterbot
with_items: '{{ ec2_twitterbots.instances }}'
- name: Wait for SSH to come up
wait_for: host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
with_items: '{{ ec2_twitterbots.instances }}'
when: server_count|int > 0
- name: Provision instance
hosts: twitterbot
remote_user: ubuntu
tasks:
- name: Install packages
apt: name={{ item }} state=present
with_items:
- nodejs
- npm
become: True
become_method: sudo
register: apt_result
until: apt_result.failed is undefined
retries: 150
delay: 5
- name: Create symlink to nodejs
shell: ln -s /usr/bin/nodejs /usr/bin/node
become: True
become_method: sudo
- name: Upload bot script
copy:
src: "{{ item }}"
dest: /home/ubuntu/
with_items:
- bot/bot.js
- bot/package.json
- twitter_secrets.json
- name: Install npm package
shell: npm install
- name: Install forever npm package
shell: npm install forever -g
become: True
become_method: sudo
# https://github.com/foreverjs/forever
- name: Start bot as daemon
shell: forever start bot.js