How to use getExtraHosts method of service Package

Best Selenoid code snippet using service.getExtraHosts

docker.go

Source:docker.go Github

copy

Full Screen

...112 Resources: ctr.Resources{113 Memory: mem,114 NanoCPUs: cpu,115 },116 ExtraHosts: getExtraHosts(d.Service, d.Caps),117 }118 hostConfig.PublishAllPorts = d.Service.PublishAllPorts119 if len(d.Caps.DNSServers) > 0 {120 hostConfig.DNS = d.Caps.DNSServers121 }122 if !d.Privileged {123 hostConfig.CapAdd = strslice.StrSlice{sysAdmin}124 }125 if len(d.ApplicationContainers) > 0 {126 hostConfig.Links = d.ApplicationContainers127 }128 if len(d.Service.Sysctl) > 0 {129 hostConfig.Sysctls = d.Service.Sysctl130 }131 cl := d.Client132 env := getEnv(d.ServiceBase, d.Caps)133 container, err := cl.ContainerCreate(ctx,134 &ctr.Config{135 Hostname: getContainerHostname(d.Caps),136 Image: image.(string),137 Env: env,138 ExposedPorts: portConfig.ExposedPorts,139 Labels: getLabels(d.Service, d.Caps),140 },141 &hostConfig,142 &network.NetworkingConfig{}, "")143 if err != nil {144 return nil, fmt.Errorf("create container: %v", err)145 }146 browserContainerStartTime := time.Now()147 browserContainerId := container.ID148 videoContainerId := ""149 log.Printf("[%d] [STARTING_CONTAINER] [%s] [%s]", requestId, image, browserContainerId)150 err = cl.ContainerStart(ctx, browserContainerId, types.ContainerStartOptions{})151 if err != nil {152 removeContainer(ctx, cl, requestId, browserContainerId)153 return nil, fmt.Errorf("start container: %v", err)154 }155 log.Printf("[%d] [CONTAINER_STARTED] [%s] [%s] [%.2fs]", requestId, image, browserContainerId, util.SecondsSince(browserContainerStartTime))156 if len(d.AdditionalNetworks) > 0 {157 for _, networkName := range d.AdditionalNetworks {158 err = cl.NetworkConnect(ctx, networkName, browserContainerId, nil)159 if err != nil {160 return nil, fmt.Errorf("failed to connect container %s to network %s: %v", browserContainerId, networkName, err)161 }162 }163 }164 stat, err := cl.ContainerInspect(ctx, browserContainerId)165 if err != nil {166 removeContainer(ctx, cl, requestId, browserContainerId)167 return nil, fmt.Errorf("inspect container %s: %s", browserContainerId, err)168 }169 _, ok := stat.NetworkSettings.Ports[selenium]170 if !ok {171 removeContainer(ctx, cl, requestId, browserContainerId)172 return nil, fmt.Errorf("no bindings available for %v", selenium)173 }174 servicePort := d.Service.Port175 pc := map[string]nat.Port{176 servicePort: selenium,177 ports.VNC: vnc,178 ports.Devtools: devtools,179 ports.Fileserver: fileserver,180 ports.Clipboard: clipboard,181 }182 hostPort := getHostPort(d.Environment, servicePort, d.Caps, stat, pc)183 u := &url.URL{Scheme: "http", Host: hostPort.Selenium, Path: d.Service.Path}184 if d.Video {185 videoContainerId, err = startVideoContainer(ctx, cl, requestId, stat, d.Environment, d.ServiceBase, d.Caps)186 if err != nil {187 return nil, fmt.Errorf("start video container: %v", err)188 }189 }190 serviceStartTime := time.Now()191 err = wait(u.String(), d.StartupTimeout)192 if err != nil {193 if videoContainerId != "" {194 stopVideoContainer(ctx, cl, requestId, videoContainerId, d.Environment)195 }196 removeContainer(ctx, cl, requestId, browserContainerId)197 return nil, fmt.Errorf("wait: %v", err)198 }199 log.Printf("[%d] [SERVICE_STARTED] [%s] [%s] [%.2fs]", requestId, image, browserContainerId, util.SecondsSince(serviceStartTime))200 log.Printf("[%d] [PROXY_TO] [%s] [%s]", requestId, browserContainerId, u.String())201 var publishedPortsInfo map[string]string202 if d.Service.PublishAllPorts {203 publishedPortsInfo = getContainerPorts(stat)204 }205 s := StartedService{206 Url: u,207 Container: &session.Container{208 ID: browserContainerId,209 IPAddress: getContainerIP(d.Environment.Network, stat),210 Ports: publishedPortsInfo,211 },212 HostPort: hostPort,213 Cancel: func() {214 if videoContainerId != "" {215 stopVideoContainer(ctx, cl, requestId, videoContainerId, d.Environment)216 }217 defer removeContainer(ctx, cl, requestId, browserContainerId)218 if d.LogOutputDir != "" && (d.SaveAllLogs || d.Log) {219 r, err := d.Client.ContainerLogs(ctx, browserContainerId, types.ContainerLogsOptions{220 Timestamps: true,221 ShowStdout: true,222 ShowStderr: true,223 })224 if err != nil {225 log.Printf("[%d] [FAILED_TO_COPY_LOGS] [%s] [Failed to capture container logs: %v]", requestId, browserContainerId, err)226 return227 }228 defer r.Close()229 filename := filepath.Join(d.LogOutputDir, d.LogName)230 f, err := os.Create(filename)231 if err != nil {232 log.Printf("[%d] [FAILED_TO_COPY_LOGS] [%s] [Failed to create log file %s: %v]", requestId, browserContainerId, filename, err)233 return234 }235 defer f.Close()236 _, err = stdcopy.StdCopy(f, f, r)237 if err != nil {238 log.Printf("[%d] [FAILED_TO_COPY_LOGS] [%s] [Failed to copy data to log file %s: %v]", requestId, browserContainerId, filename, err)239 }240 }241 },242 }243 return &s, nil244}245func getPortConfig(service *config.Browser, caps session.Caps, env Environment) (*portConfig, error) {246 selenium, err := nat.NewPort("tcp", service.Port)247 if err != nil {248 return nil, fmt.Errorf("new selenium port: %v", err)249 }250 fileserver, err := nat.NewPort("tcp", ports.Fileserver)251 if err != nil {252 return nil, fmt.Errorf("new fileserver port: %v", err)253 }254 clipboard, err := nat.NewPort("tcp", ports.Clipboard)255 if err != nil {256 return nil, fmt.Errorf("new clipboard port: %v", err)257 }258 exposedPorts := map[nat.Port]struct{}{selenium: {}, fileserver: {}, clipboard: {}}259 var vnc nat.Port260 if caps.VNC {261 vnc, err = nat.NewPort("tcp", ports.VNC)262 if err != nil {263 return nil, fmt.Errorf("new vnc port: %v", err)264 }265 exposedPorts[vnc] = struct{}{}266 }267 devtools, err := nat.NewPort("tcp", ports.Devtools)268 if err != nil {269 return nil, fmt.Errorf("new devtools port: %v", err)270 }271 exposedPorts[devtools] = struct{}{}272 portBindings := nat.PortMap{}273 if env.IP != "" || !env.InDocker {274 portBindings[selenium] = []nat.PortBinding{{HostIP: "0.0.0.0"}}275 portBindings[fileserver] = []nat.PortBinding{{HostIP: "0.0.0.0"}}276 portBindings[clipboard] = []nat.PortBinding{{HostIP: "0.0.0.0"}}277 portBindings[devtools] = []nat.PortBinding{{HostIP: "0.0.0.0"}}278 if caps.VNC {279 portBindings[vnc] = []nat.PortBinding{{HostIP: "0.0.0.0"}}280 }281 }282 return &portConfig{283 SeleniumPort: selenium,284 FileserverPort: fileserver,285 ClipboardPort: clipboard,286 VNCPort: vnc,287 DevtoolsPort: devtools,288 PortBindings: portBindings,289 ExposedPorts: exposedPorts}, nil290}291const (292 tag = "tag"293 labels = "labels"294)295func getLogConfig(logConfig ctr.LogConfig, caps session.Caps) ctr.LogConfig {296 if logConfig.Config != nil {297 _, ok := logConfig.Config[tag]298 if caps.TestName != "" && !ok {299 logConfig.Config[tag] = caps.TestName300 }301 _, ok = logConfig.Config[labels]302 if len(caps.Labels) > 0 && !ok {303 var joinedLabels []string304 for k, v := range caps.Labels {305 joinedLabels = append(joinedLabels, fmt.Sprintf("%s=%s", k, v))306 }307 logConfig.Config[labels] = strings.Join(joinedLabels, ",")308 }309 }310 return logConfig311}312func getTimeZone(service ServiceBase, caps session.Caps) *time.Location {313 timeZone := time.Local314 if caps.TimeZone != "" {315 tz, err := time.LoadLocation(caps.TimeZone)316 if err != nil {317 log.Printf("[%d] [BAD_TIMEZONE] [%s]", service.RequestId, caps.TimeZone)318 } else {319 timeZone = tz320 }321 }322 return timeZone323}324func getEnv(service ServiceBase, caps session.Caps) []string {325 env := []string{326 fmt.Sprintf("TZ=%s", getTimeZone(service, caps)),327 fmt.Sprintf("SCREEN_RESOLUTION=%s", caps.ScreenResolution),328 fmt.Sprintf("ENABLE_VNC=%v", caps.VNC),329 fmt.Sprintf("ENABLE_VIDEO=%v", caps.Video),330 }331 if caps.Skin != "" {332 env = append(env, fmt.Sprintf("SKIN=%s", caps.Skin))333 }334 if caps.VideoCodec != "" {335 env = append(env, fmt.Sprintf("CODEC=%s", caps.VideoCodec))336 }337 env = append(env, service.Service.Env...)338 env = append(env, caps.Env...)339 return env340}341func getShmSize(service *config.Browser) int64 {342 if service.ShmSize > 0 {343 return service.ShmSize344 }345 return int64(268435456)346}347func getMemory(service *config.Browser, env Environment) (int64, error) {348 if service.Mem != "" {349 var mem MemLimit350 err := mem.Set(service.Mem)351 if err != nil {352 return 0, fmt.Errorf("parse memory limit: %v", err)353 }354 return int64(mem), nil355 }356 return env.Memory, nil357}358func getCpu(service *config.Browser, env Environment) (int64, error) {359 if service.Cpu != "" {360 var cpu CpuLimit361 err := cpu.Set(service.Cpu)362 if err != nil {363 return 0, fmt.Errorf("parse CPU limit: %v", err)364 }365 return int64(cpu), nil366 }367 return env.CPU, nil368}369func getContainerHostname(caps session.Caps) string {370 if caps.ContainerHostname != "" {371 return caps.ContainerHostname372 }373 return "localhost"374}375func getExtraHosts(service *config.Browser, caps session.Caps) []string {376 extraHosts := service.Hosts377 if len(caps.HostsEntries) > 0 {378 extraHosts = append(caps.HostsEntries, extraHosts...)379 }380 return extraHosts381}382func getLabels(service *config.Browser, caps session.Caps) map[string]string {383 labels := make(map[string]string)384 if caps.TestName != "" {385 labels["name"] = caps.TestName386 }387 for k, v := range service.Labels {388 labels[k] = v389 }...

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := client.NewEnvClient()4 if err != nil {5 panic(err)6 }7 services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})8 if err != nil {9 panic(err)10 }11 for _, service := range services {12 fmt.Printf("ServiceID: %s\n", service.ID)13 fmt.Printf("Service Name: %s\n", service.Spec.Name)14 fmt.Printf("Service ExtraHosts: %s\n", service.Spec.TaskTemplate.ContainerSpec.Hosts)15 }16}

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := client.NewEnvClient()4 if err != nil {5 panic(err)6 }7 service, _, err := cli.ServiceInspectWithRaw(context.Background(), "service_name", types.ServiceInspectOptions{})8 if err != nil {9 panic(err)10 }11 fmt.Println(ExtraHosts)12}

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := client.NewEnvClient()4 if err != nil {5 panic(err)6 }7 service, _, err := cli.ServiceInspectWithRaw(context.Background(), "web", swarm.InspectServiceOptions{})8 if err != nil {9 panic(err)10 }11 fmt.Println("ExtraHosts: ", extraHosts)12}

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := GetClient()4 if err != nil {5 panic(err)6 }7 options := types.ServiceListOptions{}8 services, err := cli.ServiceList(context.Background(), options)9 if err != nil {10 panic(err)11 }12 for _, service := range services {13 fmt.Println(service.Spec.TaskTemplate.ContainerSpec.Hosts)14 }15}16import (17func main() {18 cli, err := GetClient()19 if err != nil {20 panic(err)21 }22 options := types.ServiceListOptions{}23 services, err := cli.ServiceList(context.Background(), options)24 if err != nil {25 panic(err)26 }27 for _, service := range services {28 fmt.Println(service.Spec.TaskTemplate.ContainerSpec.Hosts)29 }30}31import (32func main() {33 cli, err := GetClient()34 if err != nil {35 panic(err)36 }37 options := types.ServiceListOptions{}38 services, err := cli.ServiceList(context.Background(), options)39 if err != nil {40 panic(err)41 }42 for _, service := range services {43 fmt.Println(service.Spec.TaskTemplate.ContainerSpec.Hosts)44 }45}46import (47func main() {48 cli, err := GetClient()49 if err != nil {

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := client.NewEnvClient()4 if err != nil {5 panic(err)6 }7 service, _, err := cli.ServiceInspectWithRaw(context.Background(), "s1", types.ServiceInspectOptions{})8 if err != nil {9 panic(err)10 }11 fmt.Println("Extra Hosts: ", extraHosts)12 os.Exit(0)13}

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := client.NewEnvClient()4 if err != nil {5 log.Fatal(err)6 }7 extraHosts, err := cli.ServiceInspectWithRaw(context.Background(), "fz9d7kx2k2tj7m1kz4w7m8p6v", types.ServiceInspectOptions{})8 if err != nil {9 log.Fatal(err)10 }11 fmt.Println(extraHosts.Spec.TaskTemplate.ContainerSpec.Hosts)12}

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx := context.Background()4 cli, err := client.NewEnvClient()5 if err != nil {6 panic(err)7 }8 services, err := cli.ServiceList(ctx, types.ServiceListOptions{})9 if err != nil {10 panic(err)11 }12 for _, service := range services {13 fmt.Println(service.Spec.Name)14 for _, host := range service.Spec.TaskTemplate.ContainerSpec.Hosts {15 fmt.Println("\t", host)16 }17 }18 serviceSpec := swarm.ServiceSpec{19 Annotations: swarm.Annotations{20 },21 TaskTemplate: swarm.TaskSpec{22 ContainerSpec: &swarm.ContainerSpec{23 Hosts: []string{"

Full Screen

Full Screen

getExtraHosts

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cli, err := client.NewEnvClient()4 if err != nil {5 panic(err)6 }7 ctx := context.Background()8 spec := swarm.ServiceSpec{9 Annotations: swarm.Annotations{10 },11 TaskTemplate: swarm.TaskSpec{12 ContainerSpec: &swarm.ContainerSpec{13 Hosts: []string{

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful