Guest User

Untitled

a guest
Nov 13th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. import MySQLdb
  2. import os
  3. import sys
  4. from sqlalchemy import create_engine, MetaData
  5. from sqlalchemy.orm import scoped_session, sessionmaker
  6. from sqlalchemy import Column, String, Integer, Date, Table, ForeignKey
  7. from sqlalchemy.orm import relationship
  8. from sqlalchemy.ext.declarative import declarative_base
  9. from urllib import quote_plus as urlquote
  10. from multiprocessing.dummy import Pool as ThreadPool
  11.  
  12. # Set PID file, this prevents the script from running if already running
  13. pid = str(os.getpid())
  14. pidfile = "/tmp/server-py.pid"
  15. if os.path.isfile(pidfile):
  16. print "%s already exists, exiting" % pidfile
  17. sys.exit()
  18. file(pidfile, 'w').write(pid)
  19.  
  20. try:
  21. # Setup SQLAlchemy
  22. engine = create_engine('mysql://user:%s@localhost:3306/ghostifi' % urlquote('password@!'), echo=False)
  23. metadata = MetaData(bind=engine)
  24. Session = scoped_session(sessionmaker(engine, autoflush=True))
  25.  
  26. Base = declarative_base()
  27.  
  28. # DB classes
  29. class Subscription(Base):
  30. __tablename__ = 'wp_edd_subscriptions'
  31.  
  32. id = Column(Integer, primary_key=True)
  33. customer_id = Column(Integer)
  34. period = Column(String)
  35. initial_amount = Column(String)
  36. recurring_amount = Column(String)
  37. bill_times = Column(Integer)
  38. transaction_id = Column(String)
  39. parent_payment_id = Column(Integer)
  40. product_id = Column(Integer)
  41. created = Column(Date)
  42. expiration = Column(Date)
  43. trial_period = Column(String)
  44. status = Column(String)
  45. profile_id = Column(String)
  46. notes = Column(String)
  47. server = relationship("Server", uselist=False, backref="subscription")
  48.  
  49. class Server(Base):
  50. __tablename__ = 'servers'
  51.  
  52. id = Column(Integer, primary_key=True)
  53. customer_id = Column(Integer)
  54. product_id = Column(Integer)
  55. wp_edd_sub_id = Column(Integer, ForeignKey(Subscription.id))
  56. server_ip = Column(String)
  57. server_name = Column(String)
  58. email = Column(String)
  59. root_password = Column(String)
  60. bandwidth_this_month = Column(Integer)
  61. bandwidth_limit_this_month = Column(Integer)
  62. current_location = Column(String)
  63. rebuild_schedule = Column(String)
  64. rebuild_schedule_location = Column(String)
  65. rebuild_now_status = Column(Integer)
  66. rebuild_now_location = Column(String)
  67. ovpn_file = Column(String)
  68. status = Column(String)
  69.  
  70. def __init__(self, customer_id, product_id, wp_edd_sub_id, server_ip, server_name, email, root_password, ovpn_file, status, bandwidth_limit_this_month):
  71. self.customer_id = customer_id
  72. self.product_id = product_id
  73. self.wp_edd_sub_id = wp_edd_sub_id
  74. self.server_ip = server_ip
  75. self.server_name = server_name
  76. self.email = email
  77. self.root_password = root_password
  78. self.bandwidth_this_month = 0
  79. self.bandwidth_limit_this_month = 0
  80. self.current_location = "New Jersey"
  81. self.rebuild_schedule = "None"
  82. self.rebuild_schedule_location = "New Jersey"
  83. self.rebuild_now_status = 0
  84. self.rebuild_now_location = "New Jersey"
  85. self.ovpn_file = ovpn_file
  86. self.status = status
  87.  
  88. def create(self):
  89. print "Created!"
  90.  
  91. def destroy(self):
  92. print "Destroyed!"
  93.  
  94. def rebuild_now(self):
  95. print "Rebuilt!"
  96.  
  97. # Setup SQLAlchemy stuff
  98. Base.metadata.create_all(engine)
  99. session = Session()
  100.  
  101. # Create lists which will be populated by SQL queries
  102. servers_to_create = []
  103. servers_to_destroy = []
  104.  
  105. # Get all active subscriptions, joined to servers
  106. active_subscriptions = session.query(Subscription, Server).outerjoin(Server, Subscription.id == Server.wp_edd_sub_id).filter(Subscription.status=="Active").all()
  107.  
  108. # Find active subscriptions which do not have existing servers (create these)
  109. for subscription in active_subscriptions:
  110. # If subscription exists for this server, skip, else append to the server create list
  111. if subscription[1]:
  112. pass
  113. else:
  114. servers_to_create.append(subscription[0])
  115.  
  116. # Get all existing servers, joined to subscriptions
  117. active_servers = session.query(Server, Subscription).outerjoin(Subscription, Subscription.id == Server.wp_edd_sub_id).all()
  118.  
  119. # Find existing servers which do not have active subscriptions (destroy these)
  120. for server in active_servers:
  121. # If subscription exists for this server, skip, else append to the server destroy list
  122. if server[1]:
  123. pass
  124. else:
  125. servers_to_destroy.append(server[0])
  126.  
  127. # Get all servers which need to be rebuilt now (rebuild these)
  128. servers_to_rebuild_now = session.query(Server).filter(Server.rebuild_now_status==1).all()
  129.  
  130. # Make the Pool of workers
  131. pool = ThreadPool(4)
  132.  
  133. def thread_servers_to_create(self):
  134. # Setup SQLAlchemy
  135. session = Session()
  136. new_server = Server(self.customer_id, self.product_id, self.id, '0.0.0.0', '', '', '', '', 'Installing', 0)
  137. # Save to database
  138. session.add(new_server)
  139. session.commit()
  140. # Close session
  141. session.close()
  142. new_server.create()
  143.  
  144. def thread_servers_to_destroy(self):
  145. self.destroy()
  146.  
  147. def thread_servers_to_rebuild_now(self):
  148. self.rebuild_now()
  149.  
  150. # Start a thread for each servers_to_create
  151. results = pool.map(thread_servers_to_create, servers_to_create)
  152.  
  153. # Start a thread for each servers_to_destroy
  154. results = pool.map(thread_servers_to_destroy, servers_to_destroy)
  155.  
  156. # Start a thread for each servers_to_rebuild_now
  157. results = pool.map(thread_servers_to_rebuild_now, servers_to_rebuild_now)
  158.  
  159. # close the pool and wait for the work to finish
  160. pool.close()
  161. pool.join()
  162.  
  163. finally:
  164. os.unlink(pidfile)
Add Comment
Please, Sign In to add comment