Exemple #1
0
 public Driver(DriverConfig Config, string Image)
 {
     this.Config     = Config;
     this.Image      = Image;
     _requestsBehind = new TDict <string, DataRequest>();
     _connector      = new Connector(Config);
 }
Exemple #2
0
        public static Driver MakeDriver(string image, int replica)
        {
            string       name   = Params.ID + "_" + image.Replace("-", "_").Replace("/", "_").Replace(" ", "") + "_" + replica.ToString();
            DriverConfig config = new DriverConfig()
            {
                ID             = name,
                Host           = Params.DOCKER_HOST,
                Port           = Params.GetUnusedPort(),
                AssociatedNode = Credentials.Self,
                Image          = image,
                Replica        = replica
            };

            try
            {
                (bool running, string id)info = Docker.Instance.IsContainerRunning(name).GetAwaiter().GetResult();

                if (info.running == true && info.id != null)
                {
                    Docker.Instance.StopContainers(info.id).GetAwaiter().GetResult();
                    Docker.Instance.RemoveStoppedContainers().GetAwaiter().GetResult();
                }
            } catch { }

            while (true)
            {
                try
                {
                    Docker.Instance.Containerize(config).GetAwaiter().GetResult();
                    break;
                }
                catch
                {
                    Utils.Sleep(Utils.GetRandomInt(Params.ELECTION_TIMEOUT_START, Params.ELECTION_TIMEOUT_END));
                    (bool running, string id)info = Docker.Instance.IsContainerRunning(name).GetAwaiter().GetResult();
                    // Console.WriteLine("pulled" + info.id + " " + info.running.ToString());
                    if (info.running)
                    {
                        break;
                    }
                }
            }
            return(new Driver(config, image));
        }
Exemple #3
0
 public Connector(DriverConfig config)
 {
     Host = config.Host;
     Port = config.Port;
     ID   = config.ID;
 }
Exemple #4
0
        public async Task <string> Containerize(DriverConfig config)
        {
            string ID = null;

            try
            {
                bool status = await PullImage(config.Image);

                // Console.WriteLine(status);
                if (!status)
                {
                    return(ID);
                }

                // CancellationTokenSource cts = new CancellationTokenSource(8000);
                // cts.CancelAfter(8000);
                // Task.Run(async () => {
                // Console.WriteLine(ID);
                var response = await _client.Containers.CreateContainerAsync(new CreateContainerParameters
                {
                    Image    = config.Image,
                    Name     = config.ID,
                    Hostname = "localhost",
                    Env      = new string[] {
                        "HOST=" + config.Host,
                        "PORT=" + config.Port.ToString(),
                        "ID=" + config.ID,
                        "TIMEOUT=" + Params.TIMEOUT.ToString(),
                        "IMAGE=" + config.Image,
                        "BATCH_SIZE=" + Params.BATCH_SIZE.ToString(),
                        "NODE_HOST=" + config.AssociatedNode.Host,
                        "NODE_NAME=" + config.AssociatedNode.ID,
                        "NODE_PORT=" + config.AssociatedNode.Port.ToString()
                    },
                    ExposedPorts = new Dictionary <string, EmptyStruct>
                    {
                        {
                            config.Port.ToString(), default(EmptyStruct)
                        }
                    },
                    HostConfig = new HostConfig
                    {
                        PortBindings = new Dictionary <string, IList <PortBinding> >
                        {
                            { config.Port.ToString(), new List <PortBinding> {
                                  new PortBinding {
                                      HostPort = config.Port.ToString()
                                  }
                              } }
                        },
                        PublishAllPorts = true
                    }
                });

                Logger.Write(Logger.Tag.INFO, "Created container: " + response.ID);
                ID = response.ID;

                // #pragma warning disable CS4014
                // _client.Containers.StartContainerAsync(response.ID, null, cts.Token).GetAwaiter().GetResult();
                _client.Containers.StartContainerAsync(response.ID, null).GetAwaiter().GetResult();

                Logger.Write(Logger.Tag.INFO, "Started container: " + response.ID);

                (bool running, string ID)info = await IsContainerRunning(config.ID);

                ID     = info.ID;
                status = info.running;

                Logger.Write(Logger.Tag.COMMIT, "Container now running: " + response.ID);
                // // }).Wait(8000, cts.Token);
                // }).Wait(8000);

                if (ID == null || !status)
                {
                    Logger.Write(Logger.Tag.ERROR, "Did not manage to spin up container.");
                }
                // else CurrentState.Instance.SetHIEVar(true);

                return(ID);
            } catch
            {
                CurrentState.Instance.SetHIEVar(false);
                return(ID);
            }
        }