From b2d12b63eef9905357bc873a0cda5d10a0d91ebc Mon Sep 17 00:00:00 2001 From: MarollaVictor Date: Sat, 4 Jan 2025 12:37:10 +0200 Subject: [PATCH 1/6] updated readme file --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ce462902d67..a5edbb3d4ba5 100644 --- a/README.md +++ b/README.md @@ -138,5 +138,5 @@ Usage: .update(<_id>, ) (hbnb) (hbnb) User.all() (hbnb) ["[User] (98bea5de-9cb0-4d78-8a9d-c4de03521c30) {'updated_at': datetime.datetime(2020, 2, 19, 21, 47, 29, 134362), 'name': 'Fred the Frog', 'age': 9, 'id': '98bea5de-9cb0-4d78-8a9d-c4de03521c30', 'created_at': datetime.datetime(2020, 2, 19, 21, 47, 29, 134343)}"] -``` -
\ No newline at end of file +(TBD - contributions will be updated once known).``` +
From a1b92069c9f26c3e17fb18e7878dedd450129623 Mon Sep 17 00:00:00 2001 From: MarollaVictor Date: Sun, 5 Jan 2025 11:40:36 +0200 Subject: [PATCH 2/6] console improvements --- file.json | 1 + models/__pycache__/__init__.cpython-310.pyc | Bin 0 -> 310 bytes models/__pycache__/amenity.cpython-310.pyc | Bin 0 -> 409 bytes models/__pycache__/base_model.cpython-310.pyc | Bin 0 -> 1820 bytes models/__pycache__/city.cpython-310.pyc | Bin 0 -> 475 bytes models/__pycache__/place.cpython-310.pyc | Bin 0 -> 633 bytes models/__pycache__/review.cpython-310.pyc | Bin 0 -> 497 bytes models/__pycache__/state.cpython-310.pyc | Bin 0 -> 431 bytes models/__pycache__/user.cpython-310.pyc | Bin 0 -> 516 bytes .../engine/__pycache__/__init__.cpython-310.pyc | Bin 0 -> 148 bytes .../__pycache__/file_storage.cpython-310.pyc | Bin 0 -> 2095 bytes 11 files changed, 1 insertion(+) create mode 100644 file.json create mode 100644 models/__pycache__/__init__.cpython-310.pyc create mode 100644 models/__pycache__/amenity.cpython-310.pyc create mode 100644 models/__pycache__/base_model.cpython-310.pyc create mode 100644 models/__pycache__/city.cpython-310.pyc create mode 100644 models/__pycache__/place.cpython-310.pyc create mode 100644 models/__pycache__/review.cpython-310.pyc create mode 100644 models/__pycache__/state.cpython-310.pyc create mode 100644 models/__pycache__/user.cpython-310.pyc create mode 100644 models/engine/__pycache__/__init__.cpython-310.pyc create mode 100644 models/engine/__pycache__/file_storage.cpython-310.pyc diff --git a/file.json b/file.json new file mode 100644 index 000000000000..9e26dfeeb6e6 --- /dev/null +++ b/file.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/models/__pycache__/__init__.cpython-310.pyc b/models/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..825c48326d4faa9d1a10e48e536097d3b6084fce GIT binary patch literal 310 zcmYjMu};G<5Ve!gR?z$alf{-LK`bl?K?g<#%9Q0~;%jQ;*p+RN%F2(x$i)A6W#Si@ zxDtjw>E7u*>E7MBvOHvjyK>uqQvceCePkrAsKN=6Vl7i#Wrhz~n{kHOD{FJi-->s} znaY=}Dqha-*E)#7sV9R%+YqIVT1E^)THziZv5Ug3L}z3O;zk=>M(1UZ-?V^LJRR+M zx79(K0w~u3q+`lN6Kb@*wpg!d6v8;`cW0*Qze*F2#!2IhJ8wrO@TK5@w@B+Q5kKIfBs&VK=PS5}As literal 0 HcmV?d00001 diff --git a/models/__pycache__/amenity.cpython-310.pyc b/models/__pycache__/amenity.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..190f294f7d0bfc3663a25cb4bb048ba53b66a47c GIT binary patch literal 409 zcmYjNy-ve05VoDrrd1<>#Eu7+ERDpBP@4r7I?yeP72*Sm>^L|nQdwc+NqD8Kop=Q% z?h+(=(*4?Z-*@NYc*;=j;@#sX`aLCw5@NDIZO+kXmNU(@Fx-k}u~OQVh@3K)m);?3MU%akz08O~AxC{Bq&m)W zWkf2DQlmt-pBf{M3oX)qpL|r`h%6VyNQ>T4eguaiS8s78J7{qi%hW2H4OEv_ZVwN} zQ%v(cgklJbbv(%_2Z`rt9Vvq4MDEC>OR7GPWMm5-iKl(oO`70uGYfqwQv{r$mj`9I<3#vr{3!ARy^7IB{w#;xy?{4s&uZ#i_&Te z{0(;%V+)<0C|4T|siHDT?d0R;?~j^;M@{*%`SauE^T*A@?dGwob+uZ0xN4G={YsL9 zR>MV$V(1WOI^S6Bg3&0GE+qMb^D?QaDbCM}v93VVfEAf)>|(eNsgEvB5}9>vlDKFQ zODk}JQT<~Vr253T=&vz7%Uj+s{RxhEx6|$w1Jxd+x)>_k-pllUzK_s=l;muuy@;z# z_)1EBdw5RwWO;^rsyM-X2@!a8-rzO-AU%)AR|^?=8+@{P4TtU3i|=)vGNZ(w(P=vn zmRa=ol3(%}U^fRhwiq-YTCl|~xD03h6dtDmW;pjtX0%@@JJLDz2nEUV6QR{mE2DDs zFDpQUV_|y=gg`^{u5x_)?G?s5{%zUHN&Syk7k`R}uP#~wGtxw17ufTma-kXaGwUL7 zYmi#KjvajuqUGt2$jk5cO|TvNrE_%WDABNPz!I9Pww5WMY(^c6jY9~6kkKR z20s98y_sLaJ(%)YIA?%3r5;S4{8l2*h#Lfoab|m>JJK5Y3qpq&_f&qJy}(zb+e`Bk zCCltJMRZPQIhr>xSI_KqEf`~^XRJKHF)qIWR|`Mt(Z8NrvnA9*AikT z{tuwnljQGF+F$Mfg0ko)iT;cN6eOrgn(n$4N7+Zhto9-3>tgBa2NF5X>)^dr^JP+{q;I^XpF2iZoi8~^|S literal 0 HcmV?d00001 diff --git a/models/__pycache__/city.cpython-310.pyc b/models/__pycache__/city.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0a2ba1d7dda83620da440a927630a795b8f575bf GIT binary patch literal 475 zcmYjNu};G<5Vak*gwhU33{1!aOQfoim=P)g2^i=AOj#^r8ye)q#STdA3O~c&FneX< z7nnGwNc5yT-#z>8J)a$o1_xeYCaQOfvi|CTomK6UU0d~HG<&d)T^6%~Eu4MQvSr^e31v8J7s6|Jq>YToreP?i z;C>&1MkNMjqA)O!l7jbC0!%c}NVFQWySZjEa!Mn-A2Vs2R#c5=z7?%zSLaMLiZ!Cv zETCD|4=Lxd;yesEuREcT4>^B+5$5k^i*scqz-c#yS1QnW^?}?D$m+t+IG@{E=e2Nl zq1*hlaz#^cX>6nU>roD)wefk>Hd!oP0)74XuM|RxD5f~Y9XiXXi(uGYw@mI~IZ>H5 djsV_0`sZC|!`P4>ul1kArTI9W;vwEkegV}ca%}(r literal 0 HcmV?d00001 diff --git a/models/__pycache__/place.cpython-310.pyc b/models/__pycache__/place.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..64c239f9f8282f0018ab358629dffb0ae0fdda82 GIT binary patch literal 633 zcmZuuJ#XAF43%u%o$o7cfMn@X4P9<%40H~P#!I{8P&AttL1(c(7q;a}a!_Y&{!IRo z)=vEkokH0unxQlvK~kjN6Ro$~6+!#59ZF3J`4y7q#xVJSuy4>Qk`qN$RMRHPBT7CJ z6)Sd66ocqzoX2W*Lw57K*ZgxW3*gsU^)>LX+VD@=KI5Iy-=J{(H(o_{)Gsq>0h^#M zLgl%kl!^8d10Xp?>PS(fh;oLQA?ApMit_}qM63{N#qteeTax@_lH+Hin!8he#k*0L z)7&~aa8FF&DgP4hcQ1S*L@OH*!mosA#s^@$5#oC<>!*{25K0$VUKEuZM5X+qw_rlR zLW^H3u!X5QSLxPo+P*p9snNQz{^e74knT8DmW})&${wuqC!MJZ5QjmuRe5wisimu2 zufQ*B-Ii0hMuv9cwGYIcq59GNd7B<}18F0T?!cv2mC4#n6gUau_FamPf!e0y3NE_A z#5g!d*jMNh!s!3LgpPgt9Wx2G`P1o^iv#|-upX#Qg!QrMpOh&&4+F{Nw8Zye(>Cxi Ix}j(BBNIlVIRF3v literal 0 HcmV?d00001 diff --git a/models/__pycache__/review.cpython-310.pyc b/models/__pycache__/review.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4c7bbd10bb2b07387b2d81371313f4e4171fd2fa GIT binary patch literal 497 zcmYjOu};G<5VaFG2~{&7F*11QlBJQD5h__=p+niSSRp=WkP`GTs{f2xZ5=a6OXH{wOOf+-M1^bj)E; zY~~1iiU~+gQQwmkb-bJ*Mu8Mpj38IpHxVB(i!8&;9-ieu@#eMpor7K-S zS{FvQ(507^$(l@*N;e=jXDMocvgu%G)^3fDlY!f%yNHM|>Yj-1`9;D^gmiyAjLq)w hu$)a$#zTJp)4%pm#cm)vA2xI04$->x8J*A*{|EKrd$j-n literal 0 HcmV?d00001 diff --git a/models/__pycache__/state.cpython-310.pyc b/models/__pycache__/state.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2d39d2164989fb967314660d75beee3d64ae5c12 GIT binary patch literal 431 zcmYjNy-vh13{KMYD93d`x}6Lx9k@!&2pwHup#!&diArPslr|;lA>FO;B8?&8hkC%)f4*cXn$64jleQKTS>DyC`83P#B_QGwzgL~&s6 zK@q5MMUu((M69~9195BAT7!5nR@`KHCR%G=pz6dAwjv4b53|w%CV{RzvM&^aC90dF z0a8$;W{M&OE4T`fhWFvXvW<6al`fqVJ;{@>kECqM8l>!}Qr5#5=+C5lU6*?Ib0DQM z6~f#DU&Q)D4CePNeKIwqb!kltU3&FwvnG?3HVw$P^AsC`cB%7AF4~O^uyEps^(G>m uMs&dPgxd(A{&=id+~dA}9MF~_J|z9qj^2?kh%UyVFZ~^cww%uBJop2B=3}k^ literal 0 HcmV?d00001 diff --git a/models/__pycache__/user.cpython-310.pyc b/models/__pycache__/user.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..75bf4d6b82fef6e6b882803e36cbbdea68cc2150 GIT binary patch literal 516 zcmYjNy-ve05VjMyO{)e8i3jk&5(zaDGeV^c6Ab(;S*$d^(nwBR?4+n&;bnLwuS~oG z5)$VGsXgmE-;eLUJL@=}5R`{__wtMPmv@eZp)*I=b5xRKL{b?jS_N4^$t{tgWFJH_ z2;Rdil!Hf-4EOwLU0Pmg*(%@?R%H#A=e$t4wfxzF`Nm!(p>Dj$E!=4ds_>Cxq3FyJ zmZJiaQREIJMUEh2hyh}V7|9ShnPa~1KJA!$q62Y@DZbqCZEi~4;wVjH%4OStb>u#Y zTqH!DS0IF&2+@y?_Ed=1Hdp_dkq}ZB7#={Cm&!#OT;@$1=_aev*rw~?#<*f{ei9So zGIhp>z#mdzm67D4Y5*XPcb~v2KX4Q&bUQv_BE;GWQ8H p5hmPeUp8CfANr|5nE>P6qXTzuo%n+2s^9I}|E={5j85rU_y>=$g2Dg* literal 0 HcmV?d00001 diff --git a/models/engine/__pycache__/__init__.cpython-310.pyc b/models/engine/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ba42dea34b9e2018ddc6057aef8071e004d6557b GIT binary patch literal 148 zcmd1j<>g`kf*{_?bP)X*L?8o3AjbiSi&=m~3PUi1CZpdv5bn0ykD2l0Nj8M1wGzvYgfcuXD_EgT7Iq~B14=-M5y*0;dlCmfX4{^G zjOutGhlEJdVU9lAbko@3{Lwd zH~K6heX9o3x4Fgb$E5FYhr950d4o6M-QX<%=&9BNFnw5Cp4`!BYj8EP{0dbztU-Y< zBF_CN^*=iNwC4{qnS{lpfgbFh=2<$R0BF%r$9^Ar4s4QwoKmn$C+{4HVk}c6gGYlR z%F<9?_+Vsy{$MPnNQ?LaEzOzf7!uu9Tfq|OAjpnR#h}PLRIyyd!(WKBVAmh-bx*QH zbdykKBT;nsBDtUL2b$C1?CoxCrz_H9h)EaW2XiyKqYGt*aoj@CXwWt$t4kJlVQql( zi-tObuD!esN=c?*G=8{M(TG$=X-rL+W9Sd)){J~WzG;4KRA$9WHnnFk zTbg&tqs_vZx}ejULCKfE1_Qz_O<)18$y+4vp2G?Fd<9)!9dqIzhG%eD{!Io_gz-}J zyw_>30$a%?1tF%jpH-2;~ zck(Vot5K{JtEX$c3GsWqY8LC$jj}nTRikW7H+AGTA#$zCE8WuLtsMec+m1--{Y1Z~ zkkQXUG+PkMhY&w+5kJ&*1do>^SkE$2Y+Zb}h%4?pC!Lka&5yGXQvN^UI+R(WBP+3H zz5; zcnQVJD6n+PS5f#N6qORg($k^t+)(RvZP+~m>w(twDXKM}9|I75r1Iu7BZQ<4TutQ& zPMf>>ZUES{ZM8E0F027-EUG=9lp4B*Cf5)o))%>s0`pF>7zmUbIM!8Q9h0t?6%Vp; z%0HAclbyEC=KtfnqsxlOG>7JWU-cITGm-M)7eCm$}Sq=+%xX-^86rZO!ykudmKg S?%f*O` Date: Sun, 5 Jan 2025 11:54:41 +0200 Subject: [PATCH 3/6] console improvements --- console.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/console.py b/console.py index 13a8af68e930..1fcaf3d71743 100755 --- a/console.py +++ b/console.py @@ -73,7 +73,7 @@ def precmd(self, line): pline = pline[2].strip() # pline is now str if pline: # check for *args or **kwargs - if pline[0] is '{' and pline[-1] is'}'\ + if pline[0] == '{' and pline[-1] is'}'\ and type(eval(pline)) is dict: _args = pline else: @@ -115,16 +115,37 @@ def emptyline(self): def do_create(self, args): """ Create an object of any class""" - if not args: + try: + class_name = args.split(" ")[0] + except IndexError: + pass + if not class_name: print("** class name missing **") return - elif args not in HBNBCommand.classes: + elif class_name not in HBNBCommand.classes: print("** class doesn't exist **") return - new_instance = HBNBCommand.classes[args]() - storage.save() + # create place city_id="0001" user_id="0001" name="My_liitle_house" + all_list = args.split(" ") + + new_instance = eval(class_name)() + + for i in range(1, len(all_list)): + key, value = tuple(all_list[i].split("-")) + if value.startswith('""'): + value.strip('""').replace("_", " ") + else: + try: + value = eval(value) + except Exception: + print(f"couldn't evaluate {value}") + pass + if hasattr(new_instance, key): + setattr(new_instance, key, value) + + storage.new(new_instance) print(new_instance.id) - storage.save() + new_instance.save() def help_create(self): """ Help information for the create method """ @@ -272,7 +293,7 @@ def do_update(self, args): args.append(v) else: # isolate args args = args[2] - if args and args[0] is '\"': # check for quoted arg + if args and args[0] == '\"': # check for quoted arg second_quote = args.find('\"', 1) att_name = args[1:second_quote] args = args[second_quote + 1:] From fb6f6869ee206fef01bd2067bf8181c17aed62f4 Mon Sep 17 00:00:00 2001 From: MarollaVictor Date: Sun, 5 Jan 2025 11:58:23 +0200 Subject: [PATCH 4/6] console improvements --- console.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/console.py b/console.py index 1fcaf3d71743..548482f84b70 100755 --- a/console.py +++ b/console.py @@ -301,10 +301,10 @@ def do_update(self, args): args = args.partition(' ') # if att_name was not quoted arg - if not att_name and args[0] is not ' ': + if not att_name and args[0] != ' ': att_name = args[0] # check for quoted val arg - if args[2] and args[2][0] is '\"': + if args[2] and args[2][0] == '\"': att_val = args[2][1:args[2].find('\"', 1)] # if att_val was not quoted arg From cdfd9c72914c3b3acc262ade6cd1515f8fcc6573 Mon Sep 17 00:00:00 2001 From: MarollaVictor Date: Mon, 27 Jan 2025 19:59:56 +0200 Subject: [PATCH 5/6] prepare web servers --- 0-setup_web_static.sh | 37 +++++++++++++++++++++++++++++++++++++ README.md | 5 +++-- console.py | 11 ++++++----- setup_mysql_dev.sql | 16 ++++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100755 0-setup_web_static.sh create mode 100644 setup_mysql_dev.sql diff --git a/0-setup_web_static.sh b/0-setup_web_static.sh new file mode 100755 index 000000000000..56b1e827f544 --- /dev/null +++ b/0-setup_web_static.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# 0-setup_web_static.sh + +# Install Nginx +if ! command -v nginx &> /dev/null; then + sudo apt-get update + sudo apt-get install -y nginx +fi + +# Create necessary directories +sudo mkdir -p /data/web_static/releases/test/ +sudo mkdir -p /data/web-static/shared/ + +# Create a fake HTML file for testing +echo " + + + + ALX + +" | sudo tee /data/web_static/releases/test/index.html > /dev/null + +# Create or recreate the symbolic link +sudo ln -sf /data/web_static/releases/test/ /data/web_static/current + +# Give ownership of the /data/ folder to the ubuntu user and group +sudo chown -R ubuntu:ubuntu /data/ + +# Update the Nginx configuration +nginx_config="/etc/nginx/sites-available/default" +sudo sed -i '/listen 80 default_server;/a \\n\tlocation /hbnb_static/ {\n\t\talias /data/web_static/current/;\n\t}' $nginx_config + +# Restart Nginx to apply changes +sudo service nginx restart + +# Exit successfully +exit 0 diff --git a/README.md b/README.md index a5edbb3d4ba5..77e34419d180 100644 --- a/README.md +++ b/README.md @@ -138,5 +138,6 @@ Usage: .update(<_id>, ) (hbnb) (hbnb) User.all() (hbnb) ["[User] (98bea5de-9cb0-4d78-8a9d-c4de03521c30) {'updated_at': datetime.datetime(2020, 2, 19, 21, 47, 29, 134362), 'name': 'Fred the Frog', 'age': 9, 'id': '98bea5de-9cb0-4d78-8a9d-c4de03521c30', 'created_at': datetime.datetime(2020, 2, 19, 21, 47, 29, 134343)}"] -(TBD - contributions will be updated once known).``` -
+``` +(TBD - contributions will be updated once known). +
\ No newline at end of file diff --git a/console.py b/console.py index 548482f84b70..4d8b69373c3c 100755 --- a/console.py +++ b/console.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 """ Console Module """ +from ast import Name import cmd import sys from models.base_model import BaseModel @@ -127,9 +128,9 @@ def do_create(self, args): return # create place city_id="0001" user_id="0001" name="My_liitle_house" all_list = args.split(" ") - + new_instance = eval(class_name)() - + for i in range(1, len(all_list)): key, value = tuple(all_list[i].split("-")) if value.startswith('""'): @@ -142,7 +143,7 @@ def do_create(self, args): pass if hasattr(new_instance, key): setattr(new_instance, key, value) - + storage.new(new_instance) print(new_instance.id) new_instance.save() @@ -301,10 +302,10 @@ def do_update(self, args): args = args.partition(' ') # if att_name was not quoted arg - if not att_name and args[0] != ' ': + if not att_name and args[0] is not ' ': att_name = args[0] # check for quoted val arg - if args[2] and args[2][0] == '\"': + if args[2] and args[2][0] is '\"': att_val = args[2][1:args[2].find('\"', 1)] # if att_val was not quoted arg diff --git a/setup_mysql_dev.sql b/setup_mysql_dev.sql new file mode 100644 index 000000000000..e6fd0c4772fd --- /dev/null +++ b/setup_mysql_dev.sql @@ -0,0 +1,16 @@ +-- script that prepares a MySQL server for this project +-- A database hbnb_dev_db +-- A new user hbnb_dev (in localhost) +-- The password of hbnb_dev should be set to hbnb_dev_pwd +-- hbnb_dev should have all privileges on the database hbnb_dev_db... +-- ...(and only this database) +-- hbnb_dev should have SELECT privilege on the database performance_schema... +-- ...(and only this database) +-- If the database hbnb_dev_db or the user hbnb_dev already exists, your... +-- ...script should not fail + +CREATE DATABASE IF NOT EXISTS hbnb_dev_db; +CREATE USER IF NOT EXISTS 'hbnb_dev'@'localhost' IDENTIFIED BY 'hbnb_dev_pwd'; +GRANT ALL PRIVILEGES ON hbnb_dev_db.* TO 'hbnb_dev'@'localhost'; +GRANT SELECT ON performance_schema.* TO 'hbnb_dev'@'localhost'; +FLUSH PRIVILEGES; From 0578e44b50c01e7b68d58c1ad847d3b851657218 Mon Sep 17 00:00:00 2001 From: MarollaVictor Date: Mon, 27 Jan 2025 20:56:01 +0200 Subject: [PATCH 6/6] Airbnb --- 1-pack_web_static.py | 34 ++++++++++++++ 2-do_deploy_web_static.py | 50 +++++++++++++++++++++ 3-deploy_web_static.py | 93 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100755 1-pack_web_static.py create mode 100644 2-do_deploy_web_static.py create mode 100644 3-deploy_web_static.py diff --git a/1-pack_web_static.py b/1-pack_web_static.py new file mode 100755 index 000000000000..a5c69cb17f0c --- /dev/null +++ b/1-pack_web_static.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +from fabric.api import local +from datetime import datetime +import os + +def do_pack(): + """ + Generates a .tgz archive from the contents of the web_static folder. + """ + try: + # Create the versions folder if it doesn't exist + if not os.path.exists("versions"): + local("mkdir -p versions") + + # Generate the archive name using the current timestamp + timestamp = datetime.now().strftime("%Y%m%d%H%M%S") + archive_name = f"web_static_{timestamp}.tgz" + archive_path = f"versions/{archive_name}" + + # Create the .tgz archive + print(f"Packing web_static to {archive_path}") + result = local(f"tar -cvzf {archive_path} web_static") + + # Check if the archive was created successfully + if result.succeeded: + archive_size = os.path.getsize(archive_path) + print(f"web_static packed: {archive_path} -> {archive_size}Bytes") + return archive_path + else: + return None + except Exception as e: + print(f"An error occurred: {e}") + return None + pass diff --git a/2-do_deploy_web_static.py b/2-do_deploy_web_static.py new file mode 100644 index 000000000000..fd9a5346055d --- /dev/null +++ b/2-do_deploy_web_static.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +from fabric.api import env, put, run, local +from os.path import exists + +# Define the list of web servers +env.hosts = ['', ''] # Replace with your actual server IPs +env.user = 'ubuntu' # Replace with your SSH username +env.key_filename = 'my_ssh_private_key' # Replace with your SSH private key path + +def do_deploy(archive_path): + """ + Distributes an archive to your web servers. + """ + if not exists(archive_path): + return False + + try: + # Upload the archive to the /tmp/ directory on the web server + archive_filename = archive_path.split('/')[-1] + archive_no_ext = archive_filename.split('.')[0] + remote_tmp_path = f"/tmp/{archive_filename}" + put(archive_path, remote_tmp_path) + + # Create the target directory for the release + release_path = f"/data/web_static/releases/{archive_no_ext}" + run(f"mkdir -p {release_path}") + + # Uncompress the archive to the release directory + run(f"tar -xzf {remote_tmp_path} -C {release_path}") + + # Remove the uploaded archive from the web server + run(f"rm {remote_tmp_path}") + + # Move the contents of the web_static folder to the release directory + run(f"mv {release_path}/web_static/* {release_path}/") + + # Remove the now-empty web_static folder + run(f"rm -rf {release_path}/web_static") + + # Delete the existing symbolic link + run("rm -rf /data/web_static/current") + + # Create a new symbolic link to the new release + run(f"ln -s {release_path} /data/web_static/current") + + print("New version deployed!") + return True + except Exception as e: + print(f"An error occurred: {e}") + return False diff --git a/3-deploy_web_static.py b/3-deploy_web_static.py new file mode 100644 index 000000000000..dc6556652cd5 --- /dev/null +++ b/3-deploy_web_static.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +from fabric.api import env, local, put, run +from datetime import datetime +from os.path import exists + +# Define the list of web servers +env.hosts = ['', ''] # Replace with your actual server IPs +env.user = 'ubuntu' # Replace with your SSH username +env.key_filename = 'my_ssh_private_key' # Replace with your SSH private key path + +def do_pack(): + """ + Generates a .tgz archive from the contents of the web_static folder. + """ + try: + # Create the versions folder if it doesn't exist + if not exists("versions"): + local("mkdir -p versions") + + # Generate the archive name using the current timestamp + timestamp = datetime.now().strftime("%Y%m%d%H%M%S") + archive_name = f"web_static_{timestamp}.tgz" + archive_path = f"versions/{archive_name}" + + # Create the .tgz archive + print(f"Packing web_static to {archive_path}") + result = local(f"tar -cvzf {archive_path} web_static") + + # Check if the archive was created successfully + if result.succeeded: + archive_size = local(f"stat -c%s {archive_path}", capture=True) + print(f"web_static packed: {archive_path} -> {archive_size}Bytes") + return archive_path + else: + return None + except Exception as e: + print(f"An error occurred: {e}") + return None + +def do_deploy(archive_path): + """ + Distributes an archive to your web servers. + """ + if not exists(archive_path): + return False + + try: + # Upload the archive to the /tmp/ directory on the web server + archive_filename = archive_path.split('/')[-1] + archive_no_ext = archive_filename.split('.')[0] + remote_tmp_path = f"/tmp/{archive_filename}" + put(archive_path, remote_tmp_path) + + # Create the target directory for the release + release_path = f"/data/web_static/releases/{archive_no_ext}" + run(f"mkdir -p {release_path}") + + # Uncompress the archive to the release directory + run(f"tar -xzf {remote_tmp_path} -C {release_path}") + + # Remove the uploaded archive from the web server + run(f"rm {remote_tmp_path}") + + # Move the contents of the web_static folder to the release directory + run(f"mv {release_path}/web_static/* {release_path}/") + + # Remove the now-empty web_static folder + run(f"rm -rf {release_path}/web_static") + + # Delete the existing symbolic link + run("rm -rf /data/web_static/current") + + # Create a new symbolic link to the new release + run(f"ln -s {release_path} /data/web_static/current") + + print("New version deployed!") + return True + except Exception as e: + print(f"An error occurred: {e}") + return False + +def deploy(): + """ + Creates and distributes an archive to your web servers. + """ + # Step 1: Create the archive + archive_path = do_pack() + if not archive_path: + print("Failed to create archive.") + return False + + # Step 2: Deploy the archive + return do_deploy(archive_path)