def __init__(self, inst, fast=7, slow=25, volume=1): Strategy.__init__(self, inst) threading.Thread.__init__(self) self.fast = fast self.slow = slow self.volume = volume self.InitIndicator()
def __init__(self, inst, fast = 7, slow = 25, volume = 1): Strategy.__init__(self, inst) threading.Thread.__init__(self) self.fast = fast self.slow = slow self.volume = volume self.InitIndicator()
def run(self): player = self.process_client.login(3) map_graph = self.process_client.read_map() objects = self.process_client.read_objects() strategy = Strategy(player, map_graph, objects) if self.is_gui: self.gui = GUI(player, map_graph, objects) while player.is_alive: if self.is_gui: self.gui.turn() if ((not self.gui.paused) or self.gui.onestep): moves = strategy.get_moves() if moves: for move in moves: self.process_client.move(move) self.process_client.turn() else: moves = strategy.get_moves() if moves: for move in moves: self.process_client.move(move) self.process_client.turn() sleep(0.5) return player.is_alive # for testing
def bootstrap_sim(ticker, start, n_samples): raw_data = get_boto_client('s3').get_object(Bucket='stockdatalambda', Key=ticker + '_5.pickle') data = pickle.loads(raw_data['Body'].read()) strat = Strategy(data, start_offset=start, n_samples=n_samples) calls = strat.run() return calls
def start(): user_trade = TraderApiPy(broker_id=BROKER_ID, investor_id=INVESTOR_ID, passwd=PASSWORD, auth_code=AUTH_CODE, app_id=APP_ID) user_trade.Create(LOGS_DIR + "_trade") user_trade.RegisterFront(ADDR_TRADE) user_trade.Init() Strategy.setTraderSpi(user_trade) #print ('before init strategy') InitStrategy() user_md = MdApiPy(instruments=inst_strategy.keys(), broker_id=BROKER_ID, investor_id=INVESTOR_ID, passwd=PASSWORD) user_md.Create(LOGS_DIR + "_md") user_md.RegisterFront(ADDR_MD) user_md.Init() _date = '19700101' while True: time.sleep(60) _time = time.strftime('%H%M%S') hint_time = time.localtime(time.time()) if hint_time.tm_min % 15 == 0: print('The main thread is runing, date = %s, time = %s' % (time.strftime('%Y-%m-%d'), time.strftime('%H:%M:%S'))) if '150000' < _time < '150100' and _date < time.strftime('%Y%m%d'): TickController.saveDayBar() _date = time.strftime('%Y%m%d')
class Player: playerId = None cards = None points = None Strategy = None def __init__(self, id, board, players): self.playerId = id self.cards = [] self.points = 0 self.Strategy = Strategy(self.playerId, board, players) def do(self, do, witha=None): printv(str(self.playerId) + ' ' + do) if do == 'collect': self.collect(witha) else: self.Strategy.play(do) def addCard(self, card): self.cards.append(card) def showCards(self): print("Player " + str(self.playerId) + "'s Cards: ") print(" Grain: " + str(self.cards.count('grain'))) print(" Sheep: " + str(self.cards.count('sheep'))) print(" Wood: " + str(self.cards.count('wood'))) print(" Brick: " + str(self.cards.count('brick'))) print(" Ore: " + str(self.cards.count('ore')))
def __init__(self, inst, volume = 1): Strategy.__init__(self, inst) threading.Thread.__init__(self) self.volume = volume self.InitIndicator() self.b_limit = 0 self.s_limit = 0
def strategize_community(): """ Run only at the beginning of the Simulation Give all persons strategies """ Strategy.calculate_strategies(Person.recent_memory, int(Person.get_no_of_instances()/2)) for person in Simulation.community: Simulation.community[person].give_strategies()
def __init__(self, k, amount=1): S.__init__(self, k, amount) self.status = 'clean_hands' # clean_hands/holding_buy/holding_sell self.amount = amount self.holding = 0 self.set_name('Chase') # self.contract = None logger.info('Create Strategy %s' % self.name)
def __init__(self, symbol, fast_time, slow_time, signal_time): Strategy.__init__(self, symbol) self.fast_ema = EMA(0) self.slow_ema = EMA(0) self.signal_ema = EMA(0) self.fast_time = fast_time self.slow_time = slow_time self.signal_time = signal_time self.prev_hist = 0
def traverse_ESMCCFR(self, state, player): if state.is_terminal(): return state.get_utility(player) #default to chance player other_player = 3 - player player_turn = state.get_players_turn() possible_bets = self.available_bets.get_bets_as_numbers( state._my_contrib(player_turn), state._other_contrib(player_turn), self.abstracted) # Determine the strategy at this infoset infoset = state.get_infoset(player_turn) if infoset in self.infoset_strategy_map.keys(): strategy = self.infoset_strategy_map[infoset] else: strategy = Strategy(len(possible_bets)) self.infoset_strategy_map[infoset] = strategy player_strategy = strategy.calculate_strategy() if player_turn == player: # initialize expected value # value of a node h is the value player i expects to achieve if all players play according to given strategy, having reached h value = 0 value_bet = [0] * len(player_strategy) for bet_index, bet in enumerate(possible_bets): # need to define adding an bet to a bets, make bet class memento = state.update(bet) # Traverse each bet (per iteration of loop) (each bet changes the bets) va = self.traverse_ESMCCFR(state, player) state.reverse_update(memento) value_bet[bet_index] = va # Update the expected value value += player_strategy[bet_index] * va for bet_index in range(len(possible_bets)): # Update the cumulative regret of each bet strategy.regret_sum[bet_index] += value_bet[bet_index] - value return value elif player_turn == other_player: # Sample one bet and increment bet counter bet_index = self.get_random_bet(player_strategy) bet = possible_bets[bet_index] strategy.count[bet_index] += 1 memento = state.update(bet) val = self.traverse_ESMCCFR(state, player) state.reverse_update(memento) return val else: raise Exception('How did we get here? There are no other players')
def take_attendance(): """ Count number of people who went to the bar""" count = 0 for person in Simulation.community: if Simulation.community[person].went_to_bar(): count += 1 print(count) Strategy.evalScore(count) Simulation.eval_randoms(count) Simulation.add_to_memory(count)
def test_defult(): strategy = Strategy(start_data) next_move = strategy.move(world, game) print(next_move) test_move = Move(12, 1, 0) assert next_move.line_idx == test_move.line_idx assert next_move.speed == test_move.speed assert next_move.train_idx == test_move.train_idx
def developStrategy(self, stratfiles): stop = False count = 0 while (not stop): strat = Strategy(self.__binary, self.__param, self.__directory, self.__dumpJsonProfileFile, self.__outputFile, self.__onlyApplyingStrat, self.__onlyGenStrat, stratfiles, count, self.__execAllStrat) yield strat stop = strat.isLast() count += 1
def BT(self): self.data = DataImport('spx_vols.txt') tempData = self.data.PushData() AllDates = sorted(set(tempData['Date'])) LocalStrategy = Strategy(self.data, self.portfolio) rate = self.portfolio.PrintRate() lable = [ 'Date', 'Delta', 'Gamma', 'Vega', 'Theta', '#Call', '#Put', '#CashSpot', '$P&L', '$Notional' ] self.output.append(lable) for OneDate in AllDates: LocalStrategy.Straddles(str(OneDate), True, 'W-FRI', self.MaxNotionalRatio, self.DaysToMaturity) DataForOneDate = self.data.singleDataColumn('Date', int(OneDate)) self.portfolio.PortfolioDelta(rate, DataForOneDate) self.portfolio.PortfolioGamma(rate, DataForOneDate) self.portfolio.PortfolioVega(rate, DataForOneDate) self.portfolio.PortfolioTheta(rate, DataForOneDate) self.portfolio.PortfolioHedging(self.portfolio.PrintPortDelta(), DataForOneDate) self.portfolio.ExciseOptions(OneDate, DataForOneDate) self.portfolio.PortfolioPnL(DataForOneDate) self.portfolio.AddPortfolioPnLToNational() if self.portfolio.PrintNotional() > 0.0: temp = [ OneDate, self.portfolio.PrintPortDelta(), self.portfolio.PrintPortGamma(), self.portfolio.PrintPortVega(), self.portfolio.PrintPortTheta(), self.portfolio.VolumeOfCalls(), self.portfolio.VolumeOfPuts(), self.portfolio.VolumeOfCashSpot(), self.portfolio.TotalPortfolioPnL(), self.portfolio.PrintNotional() ] self.output.append(temp) else: temp = [ OneDate, self.portfolio.PrintPortDelta(), self.portfolio.PrintPortGamma(), self.portfolio.PrintPortVega(), self.portfolio.PrintPortTheta(), self.portfolio.VolumeOfCalls(), self.portfolio.VolumeOfPuts(), self.portfolio.VolumeOfCashSpot(), self.portfolio.TotalPortfolioPnL(), self.portfolio.PrintNotional() ] self.output.append(temp) break
def __init__(self): # Config set up. Environment overrides app.yaml with open("cfg/app.yaml", "r") as f: config = yaml.safe_load(f) config.update(os.environ) # Logger set up self._init_logger(config["log_dir"]) self._logger.info("Initializing the App") # Quik connector self._connector = WebQuikConnector(conn=config["conn"], passwd=config["passwd"], account=config["account"]) # Feed2Csv just receive price and level2 for single configured asset and write to data folder web_quik_feed = WebQuikFeed(self._connector, rabbit_host=config["rabbit_host"]) # self._feed = Feed2Csv(web_quik_feed, config.sec_class, config.sec_code) # Broker is not implemented, just a stub. web_quik_broker = WebQuikBroker(connector=self._connector, client_code=config["client_code"], trade_account=config["trade_account"], rabbit_host=config["rabbit_host"]) # Create feed, subscribe events # Todo: support making orders self._strategy = Strategy(web_quik_feed, web_quik_broker, config["sec_class"], config["sec_code"])
def create_strategy(self, dict_arguments): # 形参{'trader_id': '1601', 'user_id': '800658', 'strategy_id': '01', 'OrderAlgorithm':'01', 'list_instrument_id': ['cu1611', 'cu1610']} # 判断数据库中是否存在trader_id if self.__DBM.get_trader(dict_arguments['trader_id']) is None: print("MultiUserTradeSys.create_strategy()数据库中不存在该交易员") return False # 判断数据库中是否存在user_id if self.__DBM.get_user(dict_arguments['user_id']) is None: print("MultiUserTradeSys.create_strategy()数据库中不存在该期货账户") return False # strategy_id格式必须为两位阿拉伯数字的字符串,判断数据库中是否已经存在该strategy_id if len(dict_arguments['strategy_id']) != 2: print("MultiUserTradeSys.create_strategy()策略编码数据长度不为二", len(dict_arguments['strategy_id'])) return False print('===========================') print("CTPManager.create_strategy()创建策略实例", dict_arguments) for i in self.__list_user: if i.get_user_id().decode('utf-8') == dict_arguments['user_id']: obj_strategy = Strategy(dict_arguments, i, self.__DBM) # 创建策略实例,user实例和数据库连接实例设置为strategy的属性 i.add_strategy(obj_strategy) # 将obj_strategy添加到user实例中的list_strategy # obj_strategy.set_DBM(self.__DBM) # 将数据库连接实例设置为strategy的属性 # obj_strategy.set_user(i) # 将user设置为strategy的属性 self.__list_strategy.append(obj_strategy) # 将策略实例添加到ctp_manager对象的__list_strategy属性 # 字符串转码为二进制字符串 list_instrument_id = list() for i in dict_arguments['list_instrument_id']: list_instrument_id.append(i.encode()) # 订阅行情 self.__md.sub_market(list_instrument_id, dict_arguments['user_id'], dict_arguments['strategy_id'])
def evaluate(self, color): whiteKing = 1 whiteRook = 1 blackKing = 1 whiteMoves = [] blackMoves = [] if (color == 'white' and self.isCheckmate('black')) or (color == 'black' and self.isCheckmate('white')): return 1000000 elif (color == 'white' and self.isCheckmate('white')) or (color == 'black' and self.isCheckmate('black')): return -1000000 positionBonus = 0 for loc, square in self.squares.items(): if square.isOccupied() and square.getPiece().isAlive(): piece = square.getPiece() # Positional analysis positionBonus = Strategy.squareToValue(square, piece.getColor()) # Number of moves analysis if piece.isColor('white'): whiteMoves += square.generateMoves() else: blackMoves += square.generateMoves() # Capture Analysis for piece in self.capturedPieces: if piece.isType('King'): if piece.isColor('white'): whiteKing = 0 else: blackKing = 0 elif piece.isType('Rook') and piece.isColor('white'): whiteRook = 0 movesLookup = whiteMoves if color == 'white' else blackMoves # Evaluate the available attacks on the board and give a bonus attackBonus = self.evaluateAttackBonus(color, movesLookup) movesLookup = whiteMoves if color == 'black' else blackMoves # Evaluate the pieces under attack and subtract points for those positions underAttack = self.evaluateUnderAttack(color, movesLookup) total = 20000 * (whiteKing - blackKing) total += 500 * (whiteRook) # No black rook exists total += 10 * (len(whiteMoves) - len(blackMoves)) total += positionBonus total += attackBonus total += underAttack print('Evaluation of move = ' + str(total)) return total
def sim_ema_gra_trend_follow_opt(cls, df, opt_term, ema_term_list, ac): def __check_opt_ema_term(df, start_i, end_i, ema_term_list): pl = {} for ema_term in ema_term_list: sac = SimAccount() pl[ema_term] = cls.sim_ema_trend_follow_period( df, ema_term, start_i, end_i, sac).total_pl return max(pl.values()), max(pl, key=pl.get) print('sim length:' + str(df['dt'].iloc[0]) + str(df['dt'].iloc[-1])) for i in range(opt_term, len(df['dt']) - opt_term - 1, 1): mpl, term = __check_opt_ema_term(df, i - opt_term, i, ema_term_list) dd = Strategy.ema_gra_trend_follow(df, term, i, ac) if dd.side == '': ac.entry_order(dd.side, dd.price, dd.size, dd.type, dd.expire, 0, 0, i, df['dt'].iloc[i]) ac.move_to_next(i, df['dt'].iloc[i], df['open'].iloc[i], df['high'].iloc[i], df['low'].iloc[i], df['close'].iloc[i]) i = len(df['dt']) - opt_term - 1 ac.last_day_operation(i, df['dt'].iloc[i], df['open'].iloc[i], df['high'].iloc[i], df['low'].iloc[i], df['close'].iloc[i]) return ac
def opt_wrapper(params): """ optimizing for periods """ # assign parameters periods_bol = int(params[0]) # space from optimizer returns floats periods_adx = int(params[1]) periods_rsi = int(params[2]) adx_value = int(params[3]) ###### Define Simulations ###### data = Data(start_date="20-03-01") # historical data interfal: hours df = data.load() strategy = Strategy(df=df, periods_bol=periods_bol, periods_adx=periods_adx, periods_rsi=periods_rsi, adx_value=adx_value) account = Account(balance={"euro": 1000, "btc": 0}, av_balance=0.8) sim = Sim(strategy=strategy, account=account, stake_amount=50, stop_loss=0.02) sim_result = sim.start() # negate as optimization looks for a minimum sim_result = -sim_result["account"].balance["euro"] return sim_result
def give_strategies(self): """Give Person 6 distinct strategies at random""" while len(self.strategies) < 6: key = Strategy.get_new_key() if not key in self.strategies: self.strategies[key] = None if key == "random_attendance": self.has_random = True
def run(self): status, start_data = self.remote_process_client.login(self.name) try: map_graph = self.remote_process_client.read_map() objects = self.remote_process_client.read_objects() strategy = Strategy(start_data) while strategy.in_progress: self.remote_process_client.update_objects(objects) moves = strategy.get_moves(objects, map_graph) if moves: for move in moves: self.remote_process_client.move(move) self.remote_process_client.turn() finally: self.remote_process_client.logout() self.remote_process_client.close()
class AI(Player): def __init__(self, name, sName=None): Player.__init__(self, name) self.strat = Strategy(sName) self.strategy = self.strat.strategyName #print(self.name + " follows the " + self.strat.strategyName + " strategy.") def makeMove(self, state): return self.strat.makeDecision(state, self)
def createStrategyCombinations(historicalNodeCount): strategylist = list() for placementstrategy in ['druidcostbased', 'bestfit']: for routingstrategy in ['chooseleastloaded']: strategylist.append( Strategy(historicalNodeCount, placementstrategy, routingstrategy)) return strategylist
class AI(Player): def __init__(self, name): Player.__init__(self, name) self.strategy = Strategy() print(self.name + " follows the " + self.strategy.strategyName + " strategy.") def makeMove(self, state): decision = self.strategy.makeDecision(state, self) return decision
def __init__(self, symbol, sym, timeFrames, QCAlgorithm): self.tick = symbol self.algo = QCAlgorithm self.symbol = QCAlgorithm.Portfolio[sym] self.params = pd.Series({ 'is_long': False, 'is_short': False, 'is_sideways': False, 'sideways': np.uint8(0), 'sidewaysInit': None, ' trade': False, 'pause': False, 'strength': np.uint8(0), 'tolerance': 1.01 }) self.inds = pd.Series({ 'AL': ALMA(self.symbol, 12, 26, 6, .15, tol=self.params.tolerance), 'ATR': ATR(self.symbol, 40, 15), 'AR': AROON(self.symbol, 50) }) self.Strats = pd.Series({ 'm': Strategy(self.symbol, symbol, timeFrames['m'], QCAlgorithm), 'h': Strategy(self.symbol, symbol, timeFrames['h'], QCAlgorithm) }) self.secData = Data() self.data = pd.DataFrame({ 'openTime': [], 'open': [], 'high': [], 'low': [], 'close': [] }).set_index('openTime') self.correlation = pd.Series({'pos': {}, 'neg': {}})
def sim_ema_trend_follow(cls, stdata, ac): print('sim length:' + str(stdata.dt[0]) + str(stdata.dt[-1])) for i in range(len(stdata.prediction) - 1): dd = Strategy.ema_trend_follow(stdata, i, ac) if dd.side != '': ac.entry_order(dd.side, dd.price, dd.size, dd.type, dd.expire, i, stdata.dt[i], stdata.ut[i], stdata.price[i]) ac.move_to_next(i, stdata.dt[i], stdata.ut[i], stdata.price[i]) ac.last_day_operation(len(stdata.prediction) - 1, stdata.dt[len(stdata.prediction) - 1], stdata.ut[len(stdata.prediction) - 1], stdata.price[len(stdata.prediction) - 1]) return ac
def sim_buysell(cls, stdata, pl, ls, ac): print('sim length:' + str(stdata.dt[0]) + str(stdata.dt[-1])) for i in range(len(stdata.prediction) - 1): dd = Strategy.model_buysell_prediction(pl, ls, stdata, i, ac) if dd.side != '': ac.entry_order(dd.side, dd.price, dd.size, dd.type, dd.expire, pl, ls, i, stdata.dt[i], stdata.ut[i], stdata.price[ i]) # ntry_order(self, side, price, size, type, expire, pl, ls, i, dt, ut, tick_price): ac.move_to_next(i, stdata.dt[i], stdata.ut[i], stdata.price[i]) ac.last_day_operation(len(stdata.prediction) - 1, stdata.dt[len(stdata.prediction) - 1], stdata.ut[len(stdata.prediction) - 1], stdata.price[len(stdata.prediction) - 1]) return ac
def _read_row(row): hole = int(row[0]) board = row[1] if board == 0: board = () else: board = ((int(board), ), ) bets_0 = _comma_split_int(row[2]) bets_1 = _comma_split_int(row[3]) infoset = InfoSet((hole, ), board, (bets_0, bets_1)) strategy = Strategy(0) strategy.average_strategy = [ float(x) / 100000 for x in _comma_split_int(row[4]) ] sum_strategy = sum(strategy.average_strategy) for s in strategy.average_strategy: s /= sum_strategy return infoset, strategy
def start(): user_trade = TraderDelegate(broker_id=BROKER_ID, investor_id=INVESTOR_ID, passwd=PASSWORD) user_trade.Create(LOGS_DIR + "_trader") user_trade.RegisterFront(ADDR_TRADE) user_trade.Init() Strategy.setTraderSpi(user_trade) user_md = MdDelegate(instruments=inst_strategy.keys(), broker_id=BROKER_ID, investor_id=INVESTOR_ID, passwd=PASSWORD) user_md.Create(LOGS_DIR +"_md") user_md.RegisterFront(ADDR_MD) user_md.Init() _date = '19700101' while True: print 'in the main thread ... ' time.sleep(10) _time = time.strftime('%H%M%S') if '150000'< _time < '150020' and _date < time.strftime('%Y%m%d'): TickController.saveDayBar() _date = time.strftime('%Y%m%d')
def run(self): pair = self.pair interval = self.interval df = self.df strat = Strategy(df, pair) strat.run() #run test the entire day and live goes live strat.output(True)
def main(): ticker = raw_input("\n\n\n----------------------------------------------\nWelcome. Ready to trade? Pick a stock ticker: ") reuterObj = ReutersQuery() reuterVector = reuterObj.getQuery(ticker) sentimentObj = Sentiment() sentiments = sentimentObj.sentimentVectorize(reuterVector) yahooObj = YahooQuery() yahooVector = yahooObj.doYahooQuery(ticker, reuterVector) reuterDates = DateFormat() dates = reuterDates.fixDates(reuterVector) mergeObj = Merge() merged = mergeObj.mergeEverything(sentiments, yahooVector, dates) strategyObj = Strategy() metrics = strategyObj.runStrategy(ticker, merged) outputObj = Output() outputObj.putOutput(ticker, metrics, yahooVector, merged) print '\nThanks for trading with Vivek! Get money, get paid!'
def start(): user_trade = TraderDelegate(broker_id=BROKER_ID, investor_id=INVESTOR_ID, passwd=PASSWORD) user_trade.Create(LOGS_DIR + "_trader") user_trade.RegisterFront(ADDR_TRADE) user_trade.Init() Strategy.setTraderSpi(user_trade) InitStrategy() user_md = MdDelegate(instruments=inst_strategy.keys(), broker_id=BROKER_ID, investor_id=INVESTOR_ID, passwd=PASSWORD) user_md.Create(LOGS_DIR +"_md") user_md.RegisterFront(ADDR_MD) user_md.Init() _date = '19700101' while True: time.sleep(60) _time = time.strftime('%H%M%S') hint_time = time.localtime(time.time()) if hint_time.tm_min % 15 == 0: print 'The main thread is runing, date = %s, time = %s' % (time.strftime('%Y-%m-%d'),time.strftime('%H:%M:%S')) if '150000'< _time < '150100' and _date < time.strftime('%Y%m%d'): TickController.saveDayBar() _date = time.strftime('%Y%m%d')
def __init__(self, **kw): Strategy.__init__(self, **kw) self.preds = []
assert strategy.allQueriesRouted() #get query segment count querysegmentcount = 0 for query in allquerylist: querysegmentcount += query.getSegmentCount() #Print Statistics for strategy in dynamicstrategies: strategy.printStatistics(time, querysegmentcount) ######### STATIC SIMULATION ############# print "Static Simulation" #Creating Historical Nodes print "Creating Strategy Combinations" staticstrategy = Strategy(historicalnodecount, 'druidcostbased', 'bestfit-s', 'chooseleastloaded') for query in allquerylist: query.setStartTime(0) #Routing Queries staticstrategy.routeQueries(allquerylist, segmentrunningcount, 0) #Placing Segments allsegmentlist = [item for sublist in deepstorage.values() for item in sublist] staticstrategy.placeSegments(allsegmentlist, 0, config) #Routing Queries staticstrategy.routeQueries(list(), segmentrunningcount, 0) #Print Statistics
class Compute(object): """ Light wrapper around libcloud to facilitate integration with strategy files, and simplification of commands """ offset = 0 node = None node_specs = None node_name = None provider_name = None provider_dict = None provider_cls = None key_pair = None def __init__(self, strategy_file): self.strategy = Strategy(strategy_file) self.set_node() def __getattr__(self, attr): return getattr(self.provider_cls, attr) def set_node(self): self.provider_dict = self.strategy.get_provider(self.offset) self.provider_cls = (lambda driver: driver( region=self.provider_dict['provider']['region'], **self.provider_dict['auth'] ))(get_driver(getattr(Provider, self.provider_dict['provider']['name']))) if 'http_proxy' in environ: self.provider_cls.connection.set_http_proxy(proxy_url=environ['http_proxy']) # pp(map(obj_to_d, self.list_sizes())) get_option = partial(self.strategy.get_option, provider_name=self.provider_dict['provider']['name']) '''pp(map(node_to_dict, ifilter(lambda n: '14.04.5 x64' in n.id or '14.04.5 x64' in n.name, self.list_images())))''' self.node_specs = { 'size': get_option('hardware', self.list_sizes()), 'image': get_option('image', self.list_images()), 'location': get_option('location', self.list_locations()) } if 'create_with' in self.provider_dict: self.node_specs.update(self.provider_dict['create_with']) if 'node_password' in self.provider_dict['ssh']: self.node_specs.update({ 'auth': NodeAuthPassword(self.provider_dict['ssh']['node_password']) }) pp({self.provider_dict['provider']['name']: self.node_specs}) if 'security_group' in self.provider_dict: self.node_specs.update({'ex_securitygroup': self.provider_dict['security_group']}) if 'key_name' in self.provider_dict: self.node_specs.update({'ex_keyname': self.provider_dict['key_name']}) self.node_specs.update(dict((ex, val) for ex, val in self.provider_dict.iteritems() if ex.startswith('ex'))) def restrategise(self): self.offset += 1 self.set_node() def setup_keypair(self): try: self.import_key_pair_from_file( name=self.provider_dict['ssh']['key_name'], key_file_path=self.provider_dict['ssh']['public_key_path'] ) except NotImplementedError: logger.warn( '`import_key_pair_from_file` not implemented for {}'.format(self.provider_dict['provider']['name'])) pass # DW about it except Exception as e: if not e.message.startswith('InvalidKeyPair.Duplicate'): raise e def attempt_provision(self, create_or_deploy='create', prefer_provider=None, prefer_image=None): if ping_port() is not True: raise EnvironmentError('etcd server not up') if prefer_provider: self.strategy.strategy['provider']['options'] = (next( ifilter( lambda obj: obj.keys()[0] == prefer_provider, self.strategy.strategy['provider']['options'] ) ),) ''' # Prefer syntax self.strategy.strategy['provider']['options'].insert( 0, self.strategy.strategy['provider']['options'].pop( next( ifilter( lambda (idx, obj): obj.keys()[0] == prefer_provider, enumerate(self.strategy.strategy['provider']['options']) ) )[0] ) ) ''' for i in xrange(len(self.strategy.strategy['provider']['options'])): # Threshold logger.info('Attempting to create node "{node_name}" on: {provider}'.format( node_name=self.strategy.get_node_name(), provider=self.provider_dict['provider']['name'] )) self.provision(create_or_deploy) if self.node: save_node_info(self.node_name, node_to_dict(self.node), marshall=json) return self.node self.restrategise() raise LibcloudError('Failed to provision node') def provision(self, create_or_deploy): try: self.setup_keypair() except LibcloudError as e: logger.warn('{cls}: {msg}'.format(cls=e.__class__.__name__, msg=e.message)) if 'ex_securitygroup' in self.node_specs and self.provider_dict['provider']['name'].startswith('EC2'): print self.node_specs['ex_securitygroup'] if create_or_deploy == 'deploy': with open(self.provider_dict['ssh']['public_key_path'], mode='rt') as f: public_ssh_key = f.read() self.node_specs.update({'deploy': SSHKeyDeployment(public_ssh_key)}) self.node_name = self.strategy.get_node_name() try: self.node = getattr( self, '{0}_node'.format(create_or_deploy) )(name=self.node_name, **self.node_specs) except NotImplementedError as e: if create_or_deploy != 'deploy': raise e error_message = 'deploy_node not implemented for this driver' if e.message != error_message: raise logger.info('{error_message}, so running `create_node` instead.'.format( error_message=error_message.replace('deploy_node', '`deploy_node`') )) self.node = self.create_node(name=self.node_name, **self.node_specs) # logger.info('SoftLayer billing is giving error, will remove condition once resolved.') except LibcloudError as e: logger.warn('{cls}: {msg}'.format(cls=e.__class__.__name__, msg=e.message)) except Exception as e: if e.message.startswith('InvalidGroup.NotFound'): print 'InvalidGroup.NotFound' exit(1) else: raise e
def __init__(self, x, y): self.x, self.y = x, y Strategy.__init__(self, self)
def __init__(self, strategy_file): self.strategy = Strategy(strategy_file) self.set_node()
def __init__(self): Strategy.__init__(self, self)
def __init__(self, inst, volume = 1): Strategy.__init__(self, inst) threading.Thread.__init__(self) self.volume = volume self.InitIndicator()