Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

from charmhelpers.core import hookenv, host 

 

from charms import layer 

from pyhaproxy.parse import Parser 

from pyhaproxy.render import Render 

from crontab import CronTab 

 

import pyhaproxy.config as Config 

import reactive.letsencrypt as letsencrypt 

import subprocess 

import re 

 

 

class ProxyHelper(): 

def __init__(self): 

self.charm_config = hookenv.config() 

self.letsencrypt_config = layer.options('letsencrypt') 

self.ppa = "ppa:vbernat/haproxy-{}".format( 

self.charm_config['version']) 

self.proxy_config_file = "/etc/haproxy/haproxy.cfg" 

self._proxy_config = None 

self.domain_name = self.charm_config[ 

'letsencrypt-domains'].split(',')[0] 

self.ssl_path = '/etc/haproxy/ssl/' 

self.cert_file = self.ssl_path + self.domain_name + '.pem' 

 

@property 

def proxy_config(self): 

if not self._proxy_config: 

self._proxy_config = Parser( 

self.proxy_config_file).build_configuration() 

return self._proxy_config 

 

def add_timeout_tunnel(self, timeout='1h', save=True): 

tunnel_config = Config.Config("timeout tunnel", "{}".format(timeout)) 

defaults = self.proxy_config.defaults[0] 

for cfg in defaults.configs(): 

if cfg.keyword == 'timeout tunnel': 

defaults.remove_config(cfg) 

defaults.add_config(tunnel_config) 

if save: 

self.save_config() 

 

def get_config_names(self, configs): 

''' Note this requires a remote unit ''' 

names = [] 

for index, config in enumerate(configs): 

remote_unit = hookenv.remote_unit().replace( 

'/', '-') + '-{}'.format(index) 

backend_name = config['group_id'] or remote_unit 

names.append((remote_unit, backend_name)) 

return names 

 

def process_configs(self, configs): 

''' Note this requires a remote unit ''' 

for names, config in zip(self.get_config_names(configs), configs): 

remote_unit = names[0] 

backend_name = names[1] 

 

# For backwards compatibility and easy upgrades, 

# let's check, incase we've moved from the old <app>-<id> 

# style frontend names to the new multi-relation 

# <app>-<id>-<index> format - regex ^.*?(\d+)-(\d+)$ 

legacy = self.legacy_name(backend_name) 

if legacy != backend_name: 

hookenv.log('Cleaning any legacy configs for {} ({})'.format( 

remote_unit, 

legacy), 

'INFO') 

self.clean_config( 

unit=legacy, 

backend_name=legacy, 

save=False) 

 

# Remove any prior configuration as it might have changed 

# do not write cfg file we still have edits to make 

hookenv.log('Cleaning configs for remote {}, backend {}'.format( 

remote_unit, 

backend_name), 'DEBUG') 

self.clean_config( 

unit=remote_unit, 

backend_name=backend_name, 

save=False) 

 

# Get the frontend, create if not present 

frontend = self.get_frontend(config['external_port']) 

 

# urlbase use to accept / now they are added automatically 

# to avoid errors strip it from old configs 

if config['urlbase']: 

config['urlbase'] = config['urlbase'].rstrip('/') 

 

hookenv.log('Checking frontend {}'.format( 

str(frontend)), 'DEBUG') 

 

if config['mode'] == 'http': 

if not self.available_for_http(frontend): 

return({"cfg_good": False, 

"msg": "Port not available for http routing"}) 

 

# Add ACL's to the frontend 

if config['urlbase']: 

acl = Config.Acl( 

name=remote_unit, 

value='path_beg {}/'.format(config['urlbase'])) 

frontend.add_acl(acl) 

acl = Config.Acl(name=remote_unit, 

value='path {}'.format( 

config['urlbase'])) 

frontend.add_acl(acl) 

if config['subdomain']: 

acl = Config.Acl(name=remote_unit, 

value='hdr_beg(host) -i {}'.format( 

config['subdomain'])) 

frontend.add_acl(acl) 

 

# Add use_backend section to the frontend 

use_backend = Config.UseBackend(backend_name=backend_name, 

operator='if', 

backend_condition=remote_unit, 

is_default=False) 

frontend.add_usebackend(use_backend) 

 

if config['mode'] == 'tcp': 

if not self.available_for_tcp(frontend, backend_name): 

return({"cfg_good": False, 

"msg": ("Frontend already in use " 

"can not setup tcp mode")}) 

 

mode_config = Config.Config('mode', 'tcp') 

frontend.add_config(mode_config) 

 

# clean use backends for tcp backends, in case there is 

# any cruft left over from legacy configs 

for usebackend in frontend.usebackends(): 

frontend.remove_usebackend(usebackend.backend_name) 

 

use_backend = Config.UseBackend(backend_name=backend_name, 

operator='', 

backend_condition='', 

is_default=True) 

frontend.add_usebackend(use_backend) 

 

# Get the backend, create if not present 

backend = self.get_backend(backend_name) 

 

# Set sensible connection checking parameter 

# by default. This will work for both TCP 

# and HTTP backends, if a group-id is specified, 

# nicer HTTP checks for HTTP backends will also 

# be enabled to perform HTTP requests as part of 

# checking backend health 

attributes = [] 

if config['check']: 

attributes = ['check fall 3 rise 2'] 

 

# Add server to the backend 

# Firstly, set the mode on the backedn to match 

# the frontend 

backend.add_config(Config.Config('mode', config['mode'])) 

 

# Now, for HTTP specific configuration 

if config['mode'] == 'http': 

# Add cookie config if not already present 

cookie_found = False 

cookie = 'cookie SERVERID insert indirect nocache' 

for test_config in backend.configs(): 

if cookie in test_config.keyword: 

cookie_found = True 

if not cookie_found: 

backend.add_config(Config.Config(cookie, '')) 

attributes.append('cookie {}'.format(remote_unit)) 

# Add httpchk option if not present 

if config['group_id']: 

httpchk_found = False 

httpchk = 'httpchk GET {} HTTP/1.0'.format( 

config['urlbase'] or '/') 

for test_option in backend.options(): 

if httpchk in test_option.keyword: 

httpchk_found = True 

if not httpchk_found: 

backend.add_option(Config.Option(httpchk, '')) 

attributes.append('check') 

# Add rewrite-path if requested and not present 

if config['rewrite-path'] and config['urlbase']: 

rewrite_found = False 

rewrite = ("http-request set-path " 

"%[path,regsub(^{}/?,/)]").format( 

config['urlbase']) 

for test_cfg in backend.configs(): 

if rewrite in test_cfg.keyword: 

rewrite_found = True 

if not rewrite_found: 

backend.add_config(Config.Config(rewrite, '')) 

if config['acl-local']: 

if not backend.acl('local'): 

backend.add_acl( 

Config.Acl('local', 

("src 10.0.0.0/8 " 

"172.16.0.0/12 " 

"192.168.0.0/16 " 

"127.0.0.0/8 " 

"fd00::/8 " 

"fe80::/10 " 

"::1/128"))) 

backend.add_config( 

Config.Config('http-request deny if !local', '')) 

if config['proxypass']: 

proxy_found = False 

for test_option in backend.options(): 

if 'forwardfor' in test_cfg.keyword: 

proxy_found = True 

if not proxy_found: 

backend.add_option(Config.Option('forwardfor', '')) 

if config['external_port'] == 443: 

forward_for = ("http-request set-header " 

"X-Forwarded-Proto https") 

else: 

forward_for = ("http-request set-header " 

"X-Forwarded-Proto http") 

backend.add_config(Config.Config(forward_for, '')) 

if config['ssl']: 

if config['ssl-verify']: 

ssl_attrib = 'ssl' 

else: 

ssl_attrib = 'ssl verify none' 

attributes.append(ssl_attrib) 

server = Config.Server(name=remote_unit, 

host=config['internal_host'], 

port=config['internal_port'], 

attributes=attributes) 

backend.add_server(server) 

 

# Render new cfg file 

self.save_config() 

return({"cfg_good": True, "msg": "configuration applied"}) 

 

def available_for_http(self, frontend): 

if frontend.name == "stats": 

return False 

for config in frontend.configs(): 

if "mode" in config.keyword and\ 

"tcp" in config.value: 

return False 

return True 

 

def legacy_name(self, name): 

regex = re.compile(r'^.*?(\d+)-(\d+)$') 

matches = regex.search(name) 

if matches: 

# we are dealing with a new-style indexed relation 

# we should remove any old-style config 

index_suffix = '-{}'.format( 

matches.group(2)) 

return name.rstrip(index_suffix) 

else: 

return name 

 

def available_for_tcp(self, frontend, backend_name): 

if len(frontend.acls()): 

return False 

if len(frontend.usebackends()): 

valid_backend = False 

for ub in frontend.usebackends(): 

if ub.backend_name == backend_name: 

valid_backend = True 

# also check for legacy backend name in 

# case we've upgrades 

if ub.backend_name == self.legacy_name( 

backend_name): 

valid_backend = True 

if not valid_backend: 

return False 

return True 

 

def enable_stats(self, save=True): 

# Remove any previous stats 

self.disable_stats(save=False) 

 

# Check that no frontend exists with conflicting port 

if self.get_frontend( 

port=self.charm_config['stats-port'], 

create=False) is not None: 

hookenv.log("Stats port {} already in use".format( 

self.charm_config['stats-port']), 'ERROR') 

if save: 

self.save_config() 

return False 

 

# Generate new front end for stats 

user_string = '{}:{}'.format( 

self.charm_config['stats-user'], 

self.charm_config['stats-passwd']) 

config_block = [] 

config_block.append( 

Config.Bind( 

'0.0.0.0', self.charm_config['stats-port'], None)) 

config_block.append( 

Config.Config( 

'stats enable', '')) 

config_block.append( 

Config.Config( 

'stats auth {}'.format( 

user_string), '')) 

config_block.append( 

Config.Config( 

'stats uri {}'.format( 

self.charm_config['stats-url']), '')) 

if self.charm_config['stats-local']: 

config_block.append( 

Config.Acl('local', 

("src 10.0.0.0/8 " 

"172.16.0.0/12 " 

"192.168.0.0/16 " 

"127.0.0.0/8 " 

"fd00::/8 " 

"fe80::/10 " 

"::1/128"))) 

config_block.append( 

Config.Config( 

'http-request deny if !local', '')) 

frontend = Config.Frontend( 

'stats', 

'0.0.0.0', 

str(self.charm_config['stats-port']), 

config_block) 

self.proxy_config.frontends.append(frontend) 

if save: 

self.save_config() 

return True 

 

def disable_stats(self, save=True): 

# Remove any previous stats frontend 

self.proxy_config.frontends[:] = [ 

fe for fe in self.proxy_config.frontends if fe.name != 'stats'] 

if save: 

self.save_config() 

 

def enable_redirect(self, save=True): 

backend_name = 'redirect' 

 

# remove any prevoius configureation 

self.disable_redirect(save=False) 

 

# Get or create frontend 80 

frontend = self.get_frontend(port=80) 

 

# Add use_backend section to the frontend 

use_backend = Config.UseBackend(backend_name=backend_name, 

operator='', 

backend_condition='', 

is_default=True) 

frontend.add_usebackend(use_backend) 

 

# Get the backend, create if not present 

backend = self.get_backend(backend_name) 

 

# Add redirect option to the backend 

redirect_config = Config.Config('redirect scheme https', '') 

backend.add_config(redirect_config) 

 

# Add server so clean won't remove it 

server = Config.Server(name=backend_name, host='127.0.0.1', port=0) 

backend.add_server(server) 

 

# Render new cfg file 

if save: 

self.save_config() 

 

def disable_redirect(self, save=True): 

backend_name = 'redirect' 

 

# Remove the redirect backend 

for fe in self.proxy_config.frontends: 

fe.remove_usebackend(backend_name) 

 

# Clean the config 

self.clean_config( 

unit=backend_name, 

backend_name=backend_name, 

save=save) 

 

def get_frontend(self, port=None, create=True): 

port = str(port) 

frontend = None 

for fe in self.proxy_config.frontends: 

hookenv.log("Checking frontend for port {}".format(port), "DEBUG") 

hookenv.log("Port is: {}".format(fe.port), "DEBUG") 

if fe.port == port: 

hookenv.log("Using previous frontend", "DEBUG") 

frontend = fe 

break 

if frontend is None and create: 

hookenv.log("Creating frontend for port {}".format(port), "INFO") 

config_block = [Config.Bind('0.0.0.0', port, None)] 

frontend = Config.Frontend( 

'relation-{}'.format(port), 

'0.0.0.0', 

port, 

config_block) 

self.proxy_config.frontends.append(frontend) 

return frontend 

 

def get_backend(self, name=None, create=True): 

backend = None 

for be in self.proxy_config.backends: 

if be.name == name: 

backend = be 

if not backend and create: 

hookenv.log("Creating backend {}".format(name)) 

backend = Config.Backend(name=name, config_block=[]) 

self.proxy_config.backends.append(backend) 

return backend 

 

def clean_config(self, unit, backend_name, save=True): 

# HAProxy units can't have / character 

# replace it so it doesn't fail on a common error 

# from passing in the juju unit 

unit = unit.replace('/', '-') 

backend_name = backend_name.replace('/', '-') 

hookenv.log( 

"Cleaning unit,backend: {},{}".format( 

unit, 

backend_name), 

'DEBUG') 

 

# Remove acls and use_backend statements from frontends 

for fe in self.proxy_config.frontends: 

for ub in fe.usebackends(): 

# Match on name or condition, name is needed for TCP 

# frontends which use 'default_backend' 

if ub.backend_condition == unit or \ 

ub.backend_name == unit: 

# Direct removal from config_block b/c the name will 

# match others in a group since it isn't unique 

fe.config_block.remove(ub) 

for acl in fe.acls(): 

if acl.name == unit: 

fe.remove_acl(acl.name) 

 

# Remove server statements from backends 

for be in self.proxy_config.backends: 

for server in be.servers(): 

if server.name == unit: 

be.remove_server(server.name) 

# be.config_block.remove(server) 

 

# Remove any relation frontend if it doesn't have use_backend 

self.proxy_config.frontends[:] = [fe for fe in 

self.proxy_config.frontends if 

len(fe.usebackends()) > 0 or not fe.name.startswith('relation')] 

 

# Remove any backend with no server 

self.proxy_config.backends[:] = [ 

be for be in self.proxy_config.backends 

if len(be.servers()) > 0] 

 

if save: 

self.save_config() 

 

def save_config(self): 

# Render new cfg file 

Render(self.proxy_config).dumps_to(self.proxy_config_file) 

host.service_reload('haproxy.service') 

 

# Check the juju ports match the config 

self.update_ports() 

 

def update_ports(self): 

opened_ports = str( 

subprocess.check_output(["opened-ports"]), 

'utf-8').split('/tcp\n') 

hookenv.log("Opened ports {}".format(opened_ports), "DEBUG") 

for frontend in self.proxy_config.frontends: 

if frontend.port in opened_ports: 

if self.charm_config['enable-stats'] \ 

and self.charm_config['stats-local'] and \ 

self.charm_config['stats-port'] == int(frontend.port): 

hookenv.log("Stats port set to be closed {}".format( 

frontend.port), 

"DEBUG") 

else: 

hookenv.log("Port already open {}".format( 

frontend.port), 

"DEBUG") 

opened_ports.remove(frontend.port) 

else: 

if self.charm_config['enable-stats'] and \ 

self.charm_config['stats-local'] and \ 

self.charm_config['stats-port'] == int(frontend.port): 

hookenv.log("Not opening stats port {}".format( 

frontend.port), 

"DEBUG") 

else: 

hookenv.log("Opening {}".format(frontend.port), "DEBUG") 

hookenv.open_port(frontend.port) 

for port in opened_ports: 

if port: 

hookenv.log("Closing port {}".format(port), "DEBUG") 

hookenv.close_port(port) 

 

def enable_letsencrypt(self): 

hookenv.log("Enabling letsencrypt", "DEBUG") 

unit_name = 'letsencrypt' 

backend_name = 'letsencrypt-backend' 

 

frontend = self.get_frontend(80) 

if not self.available_for_http(frontend): 

hookenv.log("Port 80 not available for http use by letsencrypt", 

"ERROR") 

return # TODO: Should I error here, or is returning a log ok? 

 

# Only configure the rest if we haven't already done so to avoid 

# checking every change for already existing 

first_run = True 

for acl in frontend.acls(): 

if acl.name == unit_name: 

first_run = False 

if first_run: 

# Add ACL to the frontend 

acl = Config.Acl(name=unit_name, 

value='path_beg -i /.well-known/acme-challenge/') 

frontend.add_acl(acl) 

# Add usebackend 

use_backend = Config.UseBackend(backend_name=backend_name, 

operator='if', 

backend_condition=unit_name, 

is_default=False) 

frontend.add_usebackend(use_backend) 

 

# Get the backend, create if not present 

backend = self.get_backend(backend_name) 

 

# Add server to the backend 

attributes = [''] 

server = Config.Server(name=unit_name, host='127.0.0.1', 

port=self.letsencrypt_config['port'], 

attributes=attributes) 

backend.add_server(server) 

 

# Render new cfg file 

self.save_config() 

 

# Call the register function from the letsencrypt layer 

hookenv.log("Letsencrypt port: {}".format( 

self.letsencrypt_config['port']), 'DEBUG') 

hookenv.log("Letsencrypt domains: {}".format( 

self.charm_config['letsencrypt-domains']), 'DEBUG') 

if letsencrypt.register_domains() > 0: 

hookenv.log(("Failed letsencrypt registration see " 

"/var/log/letsencrypt/letsencrypt.log"), "ERROR") 

return # TODO: Should I error here or is just returning a log ok? 

 

# create the merged .pem for HAProxy 

self.merge_letsencrypt_cert() 

 

# Configure the frontend 443 

frontend = self.get_frontend(443) 

if not len(frontend.binds()[0].attributes): 

frontend.binds()[0].attributes.append('ssl crt {}'.format( 

self.cert_file)) 

if first_run: 

frontend.add_acl(acl) 

frontend.add_usebackend(use_backend) 

if self.charm_config['destination-https-rewrite']: 

frontend.add_config(Config.Config( 

'reqirep', 

'Destination:\\ https(.*) Destination:\\ http\\\\1 ')) 

self.save_config() 

 

# Add cron for renew 

self.add_cert_cron() 

 

def disable_letsencrypt(self, save=True): 

# Remove non-standard frontend configs 

frontend = self.get_frontend(443) 

frontend.binds()[0].attributes[:] = [] # Remove ssl cert attribute 

frontend.remove_config( 

'reqirep', 

'Destination:\\ https(.*) Destination:\\ http\\\\1 ') 

 

# Remove any standard config 

self.clean_config(unit='letsencrypt', 

backend_name='letsencrypt-backend', 

save=save) 

self.remove_cert_cron() 

 

def merge_letsencrypt_cert(self): 

letsencrypt_live_folder = '/etc/letsencrypt/live/{}/'.format( 

self.domain_name) 

with open(self.cert_file, 'wb') as outFile: 

with open(letsencrypt_live_folder + 'fullchain.pem', 'rb') \ 

as chainFile: 

outFile.write(chainFile.read()) 

with open(letsencrypt_live_folder + 'privkey.pem', 'rb') \ 

as privFile: 

outFile.write(privFile.read()) 

 

def renew_cert(self, full=True): 

hookenv.log("Renewing cert", "INFO") 

if full: 

# Calling a full disable/enable to clean and re-write the config 

# to catch domain changes in the charm config 

hookenv.log("Performing full domain register", "INFO") 

self.disable_letsencrypt() 

self.enable_letsencrypt() 

else: 

hookenv.log("Performing renew only", "INFO") 

letsencrypt.renew() 

# create the merged .pem for HAProxy 

self.merge_letsencrypt_cert() 

host.service_reload('haproxy.service') 

 

def renew_upnp(self): 

hookenv.log("Renewing upnp port requests", "INFO") 

# check that open ports is accurate 

self.update_ports() 

# send upnp for ports even if they were already open 

opened_ports = str( 

subprocess.check_output(["opened-ports"]), 

'utf-8').split('/tcp\n') 

opened_ports.remove('') 

for port in opened_ports: 

hookenv.log("Opening port {}".format(port), "INFO") 

hookenv.open_port(port) 

 

def release_upnp(self): 

hookenv.log("Releaseing all upnp port requests", "INFO") 

# check that open ports is accurate 

self.update_ports() 

# send upnp for ports even if they were already open 

opened_ports = str( 

subprocess.check_output(["opened-ports"]), 

'utf-8').split('/tcp\n') 

opened_ports.remove('') 

for port in opened_ports: 

hookenv.log("Closing port {}".format(port), "INFO") 

hookenv.close_port(port) 

 

def add_cron(self, action, interval): 

''' action: name of the action to run 

interval: cron interval to set ''' 

root_cron = CronTab(user='root') 

unit = hookenv.local_unit() 

directory = hookenv.charm_dir() 

action_path = directory + '/actions/{}'.format(action) 

command = "juju-run {unit} {action}".format( 

unit=unit, 

action=action_path) 

job = root_cron.new(command=command, 

comment="Charm cron for {}".format(action)) 

job.setall(interval) 

root_cron.write() 

hookenv.log("Cron added: {}".format(action), "INFO") 

 

def remove_cron(self, action): 

root_cron = CronTab(user='root') 

try: 

job = next(root_cron.find_comment( 

"Charm cron for {}".format( 

action))) 

root_cron.remove(job) 

root_cron.write() 

except StopIteration: 

hookenv.log("Cron was not present to remove", "WARN") 

pass 

hookenv.log("Cron removed: {}".format(action), "INFO") 

 

def add_cert_cron(self): 

self.add_cron('renew-cert', self.charm_config['cert-renew-interval']) 

 

def remove_cert_cron(self): 

self.remove_cron('renew-cert') 

 

def add_upnp_cron(self): 

self.add_cron('renew-upnp', self.charm_config['upnp-renew-interval']) 

 

def remove_upnp_cron(self): 

self.remove_cron('renew-upnp')