def _run(self, user, commit=False): r = StringIO.StringIO() if self.maintenance and commit: maintenance.on() try: self.validate() if commit: call_command(self.tool, *self.args, commit=True, stdout=r, **self.kwargs) else: call_command(self.tool, *self.args, stdout=r, **self.kwargs) self.result = r.getvalue() except Exception as inst: self.result = "[error] {}".format(inst) self.status = 1 finally: if self.maintenance and commit: maintenance.off() if commit: CommandLineTool.objects.create(user=user, tool=self.tool, description=self.description, status="done", arguments=json.dumps({ "args": self.args, "kwargs": self.kwargs }), result=self.result) return self.result
def test_signup(self): """ user signup should be blocked during maintenance """ maintenance.on() client = Client() resp = client.post("/register", data={}) assert resp.status_code == 503 assert "maintenance mode" in resp.content.decode() maintenance.off()
def test_api(self): """ test that maintenance mode on blocks all write ops to the rest api """ # set maintenance on maintenance.on() # init api client self.client = APIClient() self.client.force_authenticate(self.superuser) err_str = "in maintenance mode" # GET requests should work as expected r = self.client.get("/api/org/1", format="json") content = json.loads(r.content) assert r.status_code == 200 # POST should be blocked r = self.client.post("/api/net", { "org_id": 1, "name": "Test net", "asn": 9000000 }, format="json") content = json.loads(r.content) assert r.status_code == 503 assert err_str in content["meta"]["error"] net = {"id": 1} # PUT should be blocked r = self.client.put("/api/net/{}".format(net["id"]), net, format="json") content = json.loads(r.content) assert r.status_code == 503 assert err_str in content["meta"]["error"] # DELETE should be blocked r = self.client.delete("/api/net/{}".format(net["id"]), {}, format="json") content = json.loads(r.content) assert r.status_code == 503 assert err_str in content["meta"]["error"] # set maintenance mode off maintenance.off()
def handle(self, *args, **options): self.commit = options.get("commit", False) self.state = options.get("state") if not settings.TUTORIAL_MODE: self.log("Command cannot be run on environment's that are not in tutorial mode. "\ " Maintenance mode "\ " is currently implemented to the extent that it is required to facilitate"\ " an environment reset on `tutorial` type servers and probably needs more work"\ " to be useful on production.") return self.log("Setting maintenance mode {}".format(self.state)) if self.state == "on": maintenance.on() else: maintenance.off()