aboutsummaryrefslogtreecommitdiff
path: root/python/lifecycle.py
blob: 39e16381f0e5d6ebf860b7dcd90bc47bf681bf13 (plain) (blame)
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
#
# (C) Copyright IBM Corp. 2017
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import os.path
import time

from config import *
from toree_manager import *

class LifecycleManager:
    configManager = None
    toreeManager = None

    def __init__(self):
        self.configManager = ConfigManager()
        self.toreeManager = ToreeManager()

    def _reserve_profile(self):
        """
        Tries to reserv a profile configuration to access Toree
        :return: the reserved profile folder location, or None
        """
        profilesFolder = self.configManager.getProfilesFolder()
        profile = None
        """Lock mutext to avoid two processes to select same kernel config"""
        # mutex = open(self.configManager.getProfilesFolder() + "/mutex")
        # lock = fcntl.flock(mutex, fcntl.LOCK_EX)
        # print(lock)
        """Select from the available kernel configurations"""
        for (path, dirs, files) in os.walk(profilesFolder):
            for folderName in dirs:
                profile = Profile(profilesFolder + '/' + folderName)
                if profile.isAvailable():
                    profile.reserve()
                    break
        """Unlock the mutex enabling other processes to select same kernel config"""
        # fcntl.flock(self.mutex, fcntl.LOCK_UN)

        return profile

    def _release_profile(self, profile):
        profile.release()

    def start_toree(self):
        """
        Reserve a profile to use, and start a remote Toree instance with that configuration
        :return: the path to the configuration file (profile.json) to use when connecting
        """
        profile = self._reserve_profile()
        if profile is None:
            raise RuntimeError('No server resources available.')
        self.toreeManager.start_toree(profile)

        return profile

    def stop_toree(self, profile):
        self.toreeManager.stop_toree(profile)
        self._release_profile(profile)


"""
manager = LifecycleManager()
print('Starting first toree kernel')
p1 = manager.start_toree()
print(p1.profilePath)
print('Starting second toree kernel')
time.sleep(15)
#p2 = manager.start_toree()
#print p2.profilePath
#print'Stopping toree kernels'
manager.stop_toree(p1)
#manager.stop_toree(p2)
print('finished')
"""