-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprovision.yml
More file actions
171 lines (147 loc) · 4.18 KB
/
provision.yml
File metadata and controls
171 lines (147 loc) · 4.18 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
- name: Provision the database server
hosts: db
become: yes
vars:
ansible_python_interpreter: /usr/bin/python3
db_root_password: "{{ db_root_password }}"
db_name: "{{ db_name }}"
app_host: "{{ app_host }}"
db_user: "root"
tasks:
- name: Update the system
apt:
update_cache: yes
- name: Install essential packages
apt:
name:
- mysql-server
- python3
- python3-pip
- python3-dev
- default-libmysqlclient-dev
- build-essential
- pkg-config
- libmysqlclient-dev
state: present
- name: Set MySQL bind address to allow external connections
lineinfile:
path: /etc/mysql/mysql.conf.d/mysqld.cnf
regexp: "^bind-address"
line: "bind-address = 0.0.0.0"
state: present
backup: yes
- name: Install Python MySQL client
pip:
name:
- mysqlclient
- name: Ensure MySQL is running
service:
name: mysql
state: started
- name: Set MySQL root password
mysql_user:
name: root
password: "{{ db_root_password }}"
host: localhost
state: present
login_user: root
login_password: "{{ db_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: Create database
mysql_db:
name: "{{ db_name }}"
state: present
login_user: root
login_password: "{{ db_root_password }}"
- name: Grant privileges to root user for FastAPI app
mysql_user:
name: "{{ db_user }}"
host: "{{ app_host }}"
password: "{{ db_root_password }}"
priv: "*.*:ALL"
state: present
login_user: root
login_password: "{{ db_root_password }}"
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: Flush privileges
mysql_query:
login_user: root
login_password: "{{ db_root_password }}"
login_host: localhost
query: FLUSH PRIVILEGES;
- name: Load initial data from SQL file
mysql_db:
name: "{{ db_name }}"
state: import
target: /vagrant/init_data.sql
login_user: root
login_password: "{{ db_root_password }}"
- name: Restart MySQL service to apply changes
service:
name: mysql
state: restarted
- name: Provision the application server
hosts: server
become: yes
vars:
ansible_python_interpreter: /usr/bin/python3
db_host: "{{ db_host }}"
app_dir: "{{ app_dir }}"
db_user: "root"
db_password: "{{ db_root_password }}"
db_database: "{{ db_name }}"
tasks:
- name: Update the system
apt:
update_cache: yes
- name: Install essential packages
apt:
name:
- python3
- python3-pip
state: present
- name: Create application directory
file:
path: "{{ app_dir }}"
state: directory
mode: "0755"
- name: Copy application code
copy:
src: ./app/
dest: "{{ app_dir }}"
- name: Install Python dependencies from requirements.txt
pip:
requirements: "{{ app_dir }}/requirements.txt"
- name: Create .env file
copy:
dest: "{{ app_dir }}/.env"
content: |
DB_USER={{ db_user }}
DB_PASSWORD={{ db_password }}
DB_HOST={{ db_host }}
DB_DATABASE={{ db_database }}
- name: Create systemd service file for FastAPI
copy:
dest: /etc/systemd/system/fastapi.service
content: |
[Unit]
Description=FastAPI Application
After=network.target
[Service]
User=vagrant
WorkingDirectory={{ app_dir }}
ExecStart=/usr/local/bin/uvicorn main:app --host 0.0.0.0 --port 5500 --proxy-headers
Restart=always
[Install]
WantedBy=multi-user.target
- name: Reload systemd to recognize the new service
command: systemctl daemon-reload
- name: Enable FastAPI service to start on boot
systemd:
name: fastapi
enabled: yes
- name: Start FastAPI service
systemd:
name: fastapi
state: started