Guest User

Untitled

a guest
Nov 13th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. import MySQLdb
  2. import os
  3. import sys
  4. from sqlalchemy import create_engine
  5. from sqlalchemy.orm import 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.  
  11. # Set PID file, this prevents the script from running if already running
  12. pid = str(os.getpid())
  13. pidfile = "/tmp/server-py.pid"
  14. if os.path.isfile(pidfile):
  15. print "%s already exists, exiting" % pidfile
  16. sys.exit()
  17. file(pidfile, 'w').write(pid)
  18.  
  19. try:
  20. # Setup SQLAlchemy
  21. engine = create_engine('mysql://user:%s@localhost:3306/ghostifi' % urlquote('password@!'), echo=False)
  22. Session = sessionmaker(bind=engine)
  23. Base = declarative_base()
  24.  
  25. # DB classes
  26. class Subscription(Base):
  27. __tablename__ = 'wp_edd_subscriptions'
  28.  
  29. id = Column(Integer, primary_key=True)
  30. customer_id = Column(Integer)
  31. period = Column(String)
  32. initial_amount = Column(String)
  33. recurring_amount = Column(String)
  34. bill_times = Column(Integer)
  35. transaction_id = Column(String)
  36. parent_payment_id = Column(Integer)
  37. product_id = Column(Integer)
  38. created = Column(Date)
  39. expiration = Column(Date)
  40. trial_period = Column(String)
  41. status = Column(String)
  42. profile_id = Column(String)
  43. notes = Column(String)
  44. server = relationship("Server", uselist=False, backref="subscription")
  45.  
  46. class Server(Base):
  47. __tablename__ = 'servers'
  48.  
  49. id = Column(Integer, primary_key=True)
  50. product_id = Column(Integer)
  51. wp_edd_sub_id = Column(Integer, ForeignKey(Subscription.id))
  52. server_ip = Column(String)
  53. server_name = Column(String)
  54. email = Column(String)
  55. root_password = Column(String)
  56. bandwidth_this_month = Column(Integer)
  57. bandwidth_limit_this_month = Column(Integer)
  58. current_location = Column(String)
  59. rebuild_schedule = Column(String)
  60. rebuild_schedule_location = Column(String)
  61. rebuild_now_status = Column(Integer)
  62. rebuild_now_location = Column(String)
  63. ovpn_file = Column(String)
  64. status = Column(String)
  65.  
  66. def __init__(self, product_id, wp_edd_sub_id, server_ip, server_name, email, root_password, ovpn_file, status, bandwidth_limit_this_month):
  67. self.product_id = product_id
  68. self.wp_edd_sub_id = wp_edd_sub_id
  69. self.server_ip = server_ip
  70. self.server_name = server_name
  71. self.email = email
  72. self.root_password = root_password
  73. self.bandwidth_this_month = 0
  74. self.current_location = "New Jersey"
  75. self.rebuild_schedule = "None"
  76. self.rebuild_now_status = 0
  77. self.rebuild_now_location = "New Jersey"
  78. self.ovpn_file = ovpn_file
  79. self.status = status
  80.  
  81. # Setup SQLAlchemy stuff
  82. Base.metadata.create_all(engine)
  83. session = Session()
  84.  
  85. # Create lists which will be populated by SQL queries
  86. servers_to_create = []
  87. servers_to_destroy = []
  88.  
  89. # Get all active subscriptions, joined to servers
  90. active_subscriptions = session.query(Subscription, Server).outerjoin(Server, Subscription.id == Server.wp_edd_sub_id).filter(Subscription.status=="Active").all()
  91.  
  92. # Find active subscriptions which do not have existing servers (create these)
  93. for subscription in active_subscriptions:
  94. # If subscription exists for this server, skip, else append to the server create list
  95. if subscription[1]:
  96. pass
  97. else:
  98. servers_to_create.append(subscription[0])
  99.  
  100. # Get all existing servers, joined to subscriptions
  101. active_servers = session.query(Server, Subscription).outerjoin(Subscription, Subscription.id == Server.wp_edd_sub_id).all()
  102.  
  103. # Find existing servers which do not have active subscriptions (destroy these)
  104. for server in active_servers:
  105. # If subscription exists for this server, skip, else append to the server destroy list
  106. if server[1]:
  107. pass
  108. else:
  109. servers_to_destroy.append(server[0])
  110.  
  111. # Get all servers which need to be rebuilt now (rebuild these)
  112. servers_to_rebuild_now = session.query(Server).filter(Server.rebuild_now_status==1).all()
  113.  
  114. # Create servers
  115. for server in servers_to_create:
  116. pass
  117.  
  118. # Destroy servers
  119. for server in servers_to_destroy:
  120. pass
  121.  
  122. # Rebuild servers
  123. for server in servers_to_rebuild_now:
  124. pass
  125.  
  126. finally:
  127. os.unlink(pidfile)
Add Comment
Please, Sign In to add comment